Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support default-font-* properties in Live-Preview #7011

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions internal/backends/qt/qt_widgets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use i_slint_core::input::{
FocusEvent, InputEventFilterResult, InputEventResult, KeyEvent, KeyEventResult, MouseEvent,
};
use i_slint_core::item_rendering::{CachedRenderingData, ItemRenderer};
use i_slint_core::item_tree::ItemTreeVTable;
use i_slint_core::items::{Item, ItemConsts, ItemRc, ItemVTable, RenderingResult, VoidArg};
use i_slint_core::layout::{LayoutInfo, Orientation};
use i_slint_core::lengths::{LogicalLength, LogicalPoint, LogicalRect, LogicalSize};
Expand Down
2 changes: 2 additions & 0 deletions internal/backends/qt/qt_widgets/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ impl Item for NativeButton {
self: Pin<&Self>,
orientation: Orientation,
_window_adapter: &Rc<dyn WindowAdapter>,
_self_component: &vtable::VRc<i_slint_core::item_tree::ItemTreeVTable>,
_self_index: u32,
) -> LayoutInfo {
let standard_button_kind = self.actual_standard_button_kind();
let mut text: qttypes::QString = self.actual_text(standard_button_kind);
Expand Down
2 changes: 2 additions & 0 deletions internal/backends/qt/qt_widgets/checkbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ impl Item for NativeCheckBox {
self: Pin<&Self>,
orientation: Orientation,
_window_adapter: &Rc<dyn WindowAdapter>,
_self_component: &vtable::VRc<i_slint_core::item_tree::ItemTreeVTable>,
_self_index: u32,
) -> LayoutInfo {
let text: qttypes::QString = self.text().as_str().into();
let widget: NonNull<()> = SlintTypeErasedWidgetPtr::qwidget_ptr(&self.widget_ptr);
Expand Down
4 changes: 4 additions & 0 deletions internal/backends/qt/qt_widgets/combobox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ impl Item for NativeComboBox {
self: Pin<&Self>,
orientation: Orientation,
_window_adapter: &Rc<dyn WindowAdapter>,
_self_component: &vtable::VRc<i_slint_core::item_tree::ItemTreeVTable>,
_self_index: u32,
) -> LayoutInfo {
let widget: NonNull<()> = SlintTypeErasedWidgetPtr::qwidget_ptr(&self.widget_ptr);
let size = cpp!(unsafe [widget as "QWidget*"] -> qttypes::QSize as "QSize" {
Expand Down Expand Up @@ -172,6 +174,8 @@ impl Item for NativeComboBoxPopup {
self: Pin<&Self>,
_orientation: Orientation,
_window_adapter: &Rc<dyn WindowAdapter>,
_self_component: &vtable::VRc<i_slint_core::item_tree::ItemTreeVTable>,
_self_index: u32,
) -> LayoutInfo {
Default::default()
}
Expand Down
2 changes: 2 additions & 0 deletions internal/backends/qt/qt_widgets/groupbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ impl Item for NativeGroupBox {
self: Pin<&Self>,
orientation: Orientation,
_window_adapter: &Rc<dyn WindowAdapter>,
_self_component: &vtable::VRc<i_slint_core::item_tree::ItemTreeVTable>,
_self_index: u32,
) -> LayoutInfo {
let text: qttypes::QString = self.title().as_str().into();

Expand Down
2 changes: 2 additions & 0 deletions internal/backends/qt/qt_widgets/lineedit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ impl Item for NativeLineEdit {
self: Pin<&Self>,
orientation: Orientation,
_window_adapter: &Rc<dyn WindowAdapter>,
_self_component: &vtable::VRc<i_slint_core::item_tree::ItemTreeVTable>,
_self_index: u32,
) -> LayoutInfo {
let min = match orientation {
Orientation::Horizontal => self.native_padding_left() + self.native_padding_right(),
Expand Down
2 changes: 2 additions & 0 deletions internal/backends/qt/qt_widgets/listviewitem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ impl Item for NativeStandardListViewItem {
self: Pin<&Self>,
orientation: Orientation,
_window_adapter: &Rc<dyn WindowAdapter>,
_self_component: &vtable::VRc<i_slint_core::item_tree::ItemTreeVTable>,
_self_index: u32,
) -> LayoutInfo {
let index: i32 = self.index();
let item = self.item();
Expand Down
2 changes: 2 additions & 0 deletions internal/backends/qt/qt_widgets/progress_indicator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ impl Item for NativeProgressIndicator {
self: Pin<&Self>,
orientation: Orientation,
_window_adapter: &Rc<dyn WindowAdapter>,
_self_component: &vtable::VRc<i_slint_core::item_tree::ItemTreeVTable>,
_self_index: u32,
) -> LayoutInfo {
let indeterminate = self.indeterminate();
let progress =
Expand Down
2 changes: 2 additions & 0 deletions internal/backends/qt/qt_widgets/scrollview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ impl Item for NativeScrollView {
self: Pin<&Self>,
orientation: Orientation,
_window_adapter: &Rc<dyn WindowAdapter>,
_self_component: &vtable::VRc<i_slint_core::item_tree::ItemTreeVTable>,
_self_index: u32,
) -> LayoutInfo {
let min = match orientation {
Orientation::Horizontal => self.native_padding_left() + self.native_padding_right(),
Expand Down
2 changes: 2 additions & 0 deletions internal/backends/qt/qt_widgets/slider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ impl Item for NativeSlider {
self: Pin<&Self>,
orientation: Orientation,
_window_adapter: &Rc<dyn WindowAdapter>,
_self_component: &vtable::VRc<i_slint_core::item_tree::ItemTreeVTable>,
_self_index: u32,
) -> LayoutInfo {
let enabled = self.enabled();
// Slint slider supports floating point ranges, while Qt uses integer. To support (0..1) ranges
Expand Down
2 changes: 2 additions & 0 deletions internal/backends/qt/qt_widgets/spinbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ impl Item for NativeSpinBox {
self: Pin<&Self>,
orientation: Orientation,
_window_adapter: &Rc<dyn WindowAdapter>,
_self_component: &vtable::VRc<i_slint_core::item_tree::ItemTreeVTable>,
_self_index: u32,
) -> LayoutInfo {
//let value: i32 = self.value();
let data = self.data();
Expand Down
2 changes: 2 additions & 0 deletions internal/backends/qt/qt_widgets/tableheadersection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ impl Item for NativeTableHeaderSection {
self: Pin<&Self>,
orientation: Orientation,
_window_adapter: &Rc<dyn WindowAdapter>,
_self_component: &vtable::VRc<i_slint_core::item_tree::ItemTreeVTable>,
_self_index: u32,
) -> LayoutInfo {
let index: i32 = self.index();
let item = self.item();
Expand Down
4 changes: 4 additions & 0 deletions internal/backends/qt/qt_widgets/tabwidget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ impl Item for NativeTabWidget {
self: Pin<&Self>,
orientation: Orientation,
_window_adapter: &Rc<dyn WindowAdapter>,
_self_component: &vtable::VRc<i_slint_core::item_tree::ItemTreeVTable>,
_self_index: u32,
) -> LayoutInfo {
let (content_size, tabbar_size) = match orientation {
Orientation::Horizontal => (
Expand Down Expand Up @@ -355,6 +357,8 @@ impl Item for NativeTab {
self: Pin<&Self>,
orientation: Orientation,
_window_adapter: &Rc<dyn WindowAdapter>,
_self_component: &vtable::VRc<i_slint_core::item_tree::ItemTreeVTable>,
_self_index: u32,
) -> LayoutInfo {
let text: qttypes::QString = self.title().as_str().into();
let icon: qttypes::QPixmap =
Expand Down
9 changes: 4 additions & 5 deletions internal/backends/qt/qt_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -673,14 +673,14 @@ impl ItemRenderer for QtItemRenderer<'_> {
fn draw_text(
&mut self,
text: Pin<&dyn RenderText>,
_: &ItemRc,
self_rc: &ItemRc,
size: LogicalSize,
_: &CachedRenderingData,
) {
let rect: qttypes::QRectF = check_geometry!(size);
let fill_brush: qttypes::QBrush = into_qbrush(text.color(), rect.width, rect.height);
let mut string: qttypes::QString = text.text().as_str().into();
let font: QFont = get_font(text.font_request(WindowInner::from_pub(self.window)));
let font: QFont = get_font(text.font_request(self_rc));
let (horizontal_alignment, vertical_alignment) = text.alignment();
let alignment = match horizontal_alignment {
TextHorizontalAlignment::Left => key_generated::Qt_AlignmentFlag_AlignLeft,
Expand Down Expand Up @@ -850,14 +850,13 @@ impl ItemRenderer for QtItemRenderer<'_> {
fn draw_text_input(
&mut self,
text_input: Pin<&items::TextInput>,
_: &ItemRc,
self_rc: &ItemRc,
size: LogicalSize,
) {
let rect: qttypes::QRectF = check_geometry!(size);
let fill_brush: qttypes::QBrush = into_qbrush(text_input.color(), rect.width, rect.height);

let font: QFont =
get_font(text_input.font_request(&WindowInner::from_pub(self.window).window_adapter()));
let font: QFont = get_font(text_input.font_request(self_rc));
let flags = match text_input.horizontal_alignment() {
TextHorizontalAlignment::Left => key_generated::Qt_AlignmentFlag_AlignLeft,
TextHorizontalAlignment::Center => key_generated::Qt_AlignmentFlag_AlignHCenter,
Expand Down
3 changes: 2 additions & 1 deletion internal/compiler/generator/cpp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3814,8 +3814,9 @@ fn compile_builtin_function_call(
BuiltinFunction::ImplicitLayoutInfo(orient) => {
if let [llr::Expression::PropertyReference(pr)] = arguments {
let native = native_item(pr, ctx);
let item_rc = access_item_rc(pr, ctx);
format!(
"{vt}->layout_info({{{vt}, const_cast<slint::cbindgen_private::{ty}*>(&{i})}}, {o}, &{window})",
"{vt}->layout_info({{{vt}, const_cast<slint::cbindgen_private::{ty}*>(&{i})}}, {o}, &{window}, &{item_rc})",
vt = native.cpp_vtable_getter,
ty = native.class_name,
o = to_cpp_orientation(orient),
Expand Down
19 changes: 15 additions & 4 deletions internal/compiler/generator/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2111,8 +2111,11 @@ fn access_window_adapter_field(ctx: &EvaluationContext) -> TokenStream {
}

/// Given a property reference to a native item (eg, the property name is empty)
/// return tokens to the `ItemRc`
fn access_item_rc(pr: &llr::PropertyReference, ctx: &EvaluationContext) -> TokenStream {
/// return tokens to the VRc for the item tree and the item index.
fn access_item_by_item_tree_and_index(
pr: &llr::PropertyReference,
ctx: &EvaluationContext,
) -> (TokenStream, TokenStream) {
let mut ctx = ctx;
let mut component_access_tokens = quote!(_self);

Expand Down Expand Up @@ -2147,12 +2150,19 @@ fn access_item_rc(pr: &llr::PropertyReference, ctx: &EvaluationContext) -> Token
quote!(#component_access_tokens.tree_index_of_first_child.get() + #item_index_in_tree - 1)
};

quote!(&sp::ItemRc::new(#component_rc_tokens, #item_index_tokens))
(component_rc_tokens, item_index_tokens)
}
_ => unreachable!(),
}
}

/// Given a property reference to a native item (eg, the property name is empty)
/// return tokens to the `ItemRc`
fn access_item_rc(pr: &llr::PropertyReference, ctx: &EvaluationContext) -> TokenStream {
let (component_rc_tokens, item_index_tokens) = access_item_by_item_tree_and_index(pr, ctx);
quote!(&sp::ItemRc::new(#component_rc_tokens, #item_index_tokens))
}

fn compile_expression(expr: &Expression, ctx: &EvaluationContext) -> TokenStream {
match expr {
Expression::StringLiteral(s) => {
Expand Down Expand Up @@ -2849,8 +2859,9 @@ fn compile_builtin_function_call(
let item = access_member(pr, ctx);
let window_adapter_tokens = access_window_adapter_field(ctx);
item.then(|item| {
let (item_tree, item_index) = access_item_by_item_tree_and_index(pr, ctx);
quote!(
sp::Item::layout_info(#item, #orient, #window_adapter_tokens)
sp::Item::layout_info(#item, #orient, #window_adapter_tokens, &#item_tree, #item_index)
)
})
} else {
Expand Down
3 changes: 1 addition & 2 deletions internal/core/item_rendering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use crate::lengths::{
LogicalVector,
};
use crate::properties::PropertyTracker;
use crate::window::WindowInner;
use crate::{Brush, Coord, SharedString};
#[cfg(not(feature = "std"))]
use alloc::boxed::Box;
Expand Down Expand Up @@ -310,7 +309,7 @@ pub trait RenderImage {
pub trait RenderText {
fn target_size(self: Pin<&Self>) -> LogicalSize;
fn text(self: Pin<&Self>) -> SharedString;
fn font_request(self: Pin<&Self>, window: &WindowInner) -> FontRequest;
fn font_request(self: Pin<&Self>, self_rc: &ItemRc) -> FontRequest;
fn color(self: Pin<&Self>) -> Brush;
fn alignment(self: Pin<&Self>) -> (TextHorizontalAlignment, TextVerticalAlignment);
fn wrap(self: Pin<&Self>) -> TextWrap;
Expand Down
8 changes: 8 additions & 0 deletions internal/core/item_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,14 @@ impl ItemRc {
result
}

pub fn window_item(&self) -> Option<VRcMapped<ItemTreeVTable, crate::items::WindowItem>> {
let root_item_in_local_item_tree = ItemRc::new(self.item_tree.clone(), 0);

root_item_in_local_item_tree.downcast::<crate::items::WindowItem>().or_else(|| {
root_item_in_local_item_tree.parent_item().and_then(|parent| parent.window_item())
})
}

/// Visit the children of this element and call the visitor to each of them, until the visitor returns [`ControlFlow::Break`].
/// When the visitor breaks, the function returns the value. If it doesn't break, the function returns None.
fn visit_descendants_impl<R>(
Expand Down
26 changes: 25 additions & 1 deletion internal/core/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use crate::input::{
KeyEventType, MouseEvent,
};
use crate::item_rendering::{CachedRenderingData, RenderBorderRectangle};
pub use crate::item_tree::ItemRc;
pub use crate::item_tree::{ItemRc, ItemTreeVTable};
use crate::layout::LayoutInfo;
use crate::lengths::{
LogicalBorderRadius, LogicalLength, LogicalSize, LogicalVector, PointLengths, RectLengths,
Expand Down Expand Up @@ -126,6 +126,8 @@ pub struct ItemVTable {
core::pin::Pin<VRef<ItemVTable>>,
orientation: Orientation,
window_adapter: &WindowAdapterRc,
self_component: &vtable::VRc<ItemTreeVTable>,
self_index: u32,
Comment on lines +129 to +130
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not self_rc: &ItemRc, like the other functions in this vtable have?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes, this is quite annoying. The issue is that &ItemRc translates to *const ItemRc in the C signature. This is the only function that's called from the C++ code straight into the vtable where a &ItemRc is needed. But because this takes a pointer, I can't write ...->layout_info(..., &ItemRc{{ ... }) as that's taking the address of a temporary. In all the other places in the FFI we also pass this "pair".

I could alternatively change this to &ItemRc but then on the C++ code call a helper function in our private header files that stores the ItemRc on the stack and passes the address. Would you prefer that?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could alternatively change this to &ItemRc but then on the C++ code call a helper function in our private header files that stores the ItemRc on the stack and passes the address. Would you prefer that?

Yes, I think it is cleaner, don't you thik?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's indeed cleaner.

) -> LayoutInfo,

/// Event handler for mouse and touch event. This function is called before being called on children.
Expand Down Expand Up @@ -188,6 +190,8 @@ impl Item for Empty {
self: Pin<&Self>,
_orientation: Orientation,
_window_adapter: &Rc<dyn WindowAdapter>,
_self_component: &vtable::VRc<crate::item_tree::ItemTreeVTable>,
_self_index: u32,
) -> LayoutInfo {
LayoutInfo { stretch: 1., ..LayoutInfo::default() }
}
Expand Down Expand Up @@ -265,6 +269,8 @@ impl Item for Rectangle {
self: Pin<&Self>,
_orientation: Orientation,
_window_adapter: &Rc<dyn WindowAdapter>,
_self_component: &vtable::VRc<crate::item_tree::ItemTreeVTable>,
_self_index: u32,
) -> LayoutInfo {
LayoutInfo { stretch: 1., ..LayoutInfo::default() }
}
Expand Down Expand Up @@ -346,6 +352,8 @@ impl Item for BasicBorderRectangle {
self: Pin<&Self>,
_orientation: Orientation,
_window_adapter: &Rc<dyn WindowAdapter>,
_self_component: &vtable::VRc<crate::item_tree::ItemTreeVTable>,
_self_index: u32,
) -> LayoutInfo {
LayoutInfo { stretch: 1., ..LayoutInfo::default() }
}
Expand Down Expand Up @@ -446,6 +454,8 @@ impl Item for BorderRectangle {
self: Pin<&Self>,
_orientation: Orientation,
_window_adapter: &Rc<dyn WindowAdapter>,
_self_component: &vtable::VRc<crate::item_tree::ItemTreeVTable>,
_self_index: u32,
) -> LayoutInfo {
LayoutInfo { stretch: 1., ..LayoutInfo::default() }
}
Expand Down Expand Up @@ -561,6 +571,8 @@ impl Item for Clip {
self: Pin<&Self>,
_orientation: Orientation,
_window_adapter: &Rc<dyn WindowAdapter>,
_self_component: &vtable::VRc<crate::item_tree::ItemTreeVTable>,
_self_index: u32,
) -> LayoutInfo {
LayoutInfo { stretch: 1., ..LayoutInfo::default() }
}
Expand Down Expand Up @@ -659,6 +671,8 @@ impl Item for Opacity {
self: Pin<&Self>,
_orientation: Orientation,
_window_adapter: &Rc<dyn WindowAdapter>,
_self_component: &vtable::VRc<crate::item_tree::ItemTreeVTable>,
_self_index: u32,
) -> LayoutInfo {
LayoutInfo { stretch: 1., ..LayoutInfo::default() }
}
Expand Down Expand Up @@ -763,6 +777,8 @@ impl Item for Layer {
self: Pin<&Self>,
_orientation: Orientation,
_window_adapter: &Rc<dyn WindowAdapter>,
_self_component: &vtable::VRc<crate::item_tree::ItemTreeVTable>,
_self_index: u32,
) -> LayoutInfo {
LayoutInfo { stretch: 1., ..LayoutInfo::default() }
}
Expand Down Expand Up @@ -842,6 +858,8 @@ impl Item for Rotate {
self: Pin<&Self>,
_orientation: Orientation,
_window_adapter: &Rc<dyn WindowAdapter>,
_self_component: &vtable::VRc<crate::item_tree::ItemTreeVTable>,
_self_index: u32,
) -> LayoutInfo {
LayoutInfo { stretch: 1., ..LayoutInfo::default() }
}
Expand Down Expand Up @@ -969,6 +987,8 @@ impl Item for WindowItem {
self: Pin<&Self>,
_orientation: Orientation,
_window_adapter: &Rc<dyn WindowAdapter>,
_self_component: &vtable::VRc<crate::item_tree::ItemTreeVTable>,
_self_index: u32,
) -> LayoutInfo {
LayoutInfo::default()
}
Expand Down Expand Up @@ -1075,6 +1095,8 @@ impl Item for ContextMenu {
self: Pin<&Self>,
_orientation: Orientation,
_window_adapter: &Rc<dyn WindowAdapter>,
_self_component: &vtable::VRc<crate::item_tree::ItemTreeVTable>,
_self_index: u32,
) -> LayoutInfo {
LayoutInfo::default()
}
Expand Down Expand Up @@ -1157,6 +1179,8 @@ impl Item for BoxShadow {
self: Pin<&Self>,
_orientation: Orientation,
_window_adapter: &Rc<dyn WindowAdapter>,
_self_component: &vtable::VRc<crate::item_tree::ItemTreeVTable>,
_self_index: u32,
) -> LayoutInfo {
LayoutInfo { stretch: 1., ..LayoutInfo::default() }
}
Expand Down
2 changes: 2 additions & 0 deletions internal/core/items/component_container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ impl Item for ComponentContainer {
self: Pin<&Self>,
orientation: Orientation,
_window_adapter: &Rc<dyn WindowAdapter>,
_self_component: &vtable::VRc<crate::item_tree::ItemTreeVTable>,
_self_index: u32,
) -> LayoutInfo {
self.ensure_updated();
if let Some(rc) = self.item_tree.borrow().clone() {
Expand Down
2 changes: 2 additions & 0 deletions internal/core/items/flickable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ impl Item for Flickable {
self: Pin<&Self>,
_orientation: Orientation,
_window_adapter: &Rc<dyn WindowAdapter>,
_self_component: &vtable::VRc<crate::item_tree::ItemTreeVTable>,
_self_index: u32,
) -> LayoutInfo {
LayoutInfo { stretch: 1., ..LayoutInfo::default() }
}
Expand Down
Loading
Loading