diff options
author | Vika <vika@fireburn.ru> | 2025-02-19 21:21:18 +0300 |
---|---|---|
committer | Vika <vika@fireburn.ru> | 2025-02-24 04:40:48 +0300 |
commit | 1f3d2772b8bb033add005f65c2c98ff33a60e3ca (patch) | |
tree | 54ad1e3e64fe6ac13c3404c955662c227b04a02b | |
parent | b5403a6178c9f37dfeee258cc44cc3e2f4870e73 (diff) | |
download | bowl-1f3d2772b8bb033add005f65c2c98ff33a60e3ca.tar.zst |
post_editor: initialize as Grid to simplify code
Now we are setting the grid positions as part of the macro. While having a counter would be neat, we can go without it and just set all positions statically. (I wonder if the XML could've let us set columns in rows while ensuring nothing overlaps. Probably not.)
-rw-r--r-- | src/components/post_editor.rs | 94 |
1 files changed, 20 insertions, 74 deletions
diff --git a/src/components/post_editor.rs b/src/components/post_editor.rs index 7bc23b3..189c031 100644 --- a/src/components/post_editor.rs +++ b/src/components/post_editor.rs @@ -86,7 +86,7 @@ pub(crate) struct PostEditor<E> { #[do_not_track] tags: relm4::factory::FactoryVecDeque<TagPill>, visibility: Visibility, - #[do_not_track] wide_layout: gtk::GridLayout, + #[do_not_track] narrow_layout: gtk::BoxLayout, #[cfg(feature = "smart-summary")] #[do_not_track] smart_summary: Controller<crate::components::SmartSummaryButton>, @@ -137,20 +137,21 @@ impl<E: std::error::Error + std::fmt::Debug + Send + 'static> Component for Post gtk::ScrolledWindow { #[name = "content"] - gtk::Box { + gtk::Grid { set_orientation: gtk::Orientation::Vertical, - set_spacing: 5, + set_column_homogeneous: false, + set_row_spacing: 10, set_margin_all: 5, #[name = "name_label"] - gtk::Label { + attach[0, 0, 1, 1] = >k::Label { set_markup: &gettext("Name"), set_margin_horizontal: 10, set_halign: gtk::Align::Start, set_valign: gtk::Align::Center, }, #[name = "name_field"] - gtk::Entry { + attach[1, 0, 1, 1] = >k::Entry { set_hexpand: true, set_buffer: &model.name_buffer, #[track = "model.changed(Self::sending())"] @@ -158,14 +159,14 @@ impl<E: std::error::Error + std::fmt::Debug + Send + 'static> Component for Post }, #[name = "summary_label"] - gtk::Label { + attach[0, 1, 1, 1] = >k::Label { set_markup: &gettext("Summary"), set_margin_horizontal: 10, set_halign: gtk::Align::Start, set_valign: gtk::Align::Center, }, #[name = "summary_field"] - gtk::Box { + attach[1, 1, 1, 1] = >k::Box { set_orientation: gtk::Orientation::Horizontal, add_css_class: "linked", @@ -178,14 +179,14 @@ impl<E: std::error::Error + std::fmt::Debug + Send + 'static> Component for Post }, #[name = "tag_label"] - gtk::Label { + attach[0, 2, 1, 1] = >k::Label { set_markup: &gettext("Tags"), set_margin_horizontal: 10, set_halign: gtk::Align::Start, set_valign: gtk::Align::Center, }, #[name = "tag_holder"] - gtk::Box { + attach[1, 2, 1, 1] = >k::Box { set_hexpand: true, set_orientation: gtk::Orientation::Vertical, set_spacing: 5, @@ -211,10 +212,10 @@ impl<E: std::error::Error + std::fmt::Debug + Send + 'static> Component for Post }, }, - model.tags.widget(), + attach[1, 3, 1, 1] = model.tags.widget(), #[name = "content_label"] - gtk::Label { + attach[0, 4, 1, 1] = >k::Label { set_markup: &gettext("Content"), set_halign: gtk::Align::Start, set_valign: gtk::Align::Start, @@ -223,7 +224,7 @@ impl<E: std::error::Error + std::fmt::Debug + Send + 'static> Component for Post }, #[name = "content_textarea_wrapper"] - gtk::ScrolledWindow { + attach[1, 4, 1, 1] = >k::ScrolledWindow { set_vexpand: true, set_height_request: 200, #[name = "content_textarea"] @@ -248,7 +249,7 @@ impl<E: std::error::Error + std::fmt::Debug + Send + 'static> Component for Post }, #[name = "misc_prop_wrapper"] - gtk::Box { + attach[0, 5, 2, 1] = >k::Box { set_hexpand: true, gtk::FlowBox { @@ -308,17 +309,12 @@ impl<E: std::error::Error + std::fmt::Debug + Send + 'static> Component for Post // change some properties along the way. add_breakpoint = adw::Breakpoint::new( adw::BreakpointCondition::new_length( - adw::BreakpointConditionLengthType::MinWidth, + adw::BreakpointConditionLengthType::MaxWidth, 512.0, adw::LengthUnit::Px ) ) { - add_setter: (&content, "layout_manager", Some(&model.wide_layout.to_value())), - add_setter: (&name_label, "halign", Some(>k::Align::End.to_value())), - add_setter: (&summary_label, "halign", Some(>k::Align::End.to_value())), - add_setter: (&tag_label, "halign", Some(>k::Align::End.to_value())), - add_setter: (&content_label, "halign", Some(>k::Align::End.to_value())), - add_setter: (&pending_tag_entry, "hexpand", Some(&false.to_value())), + add_setter: (&content, "layout_manager", Some(&model.narrow_layout.to_value())), }, } @@ -359,7 +355,10 @@ impl<E: std::error::Error + std::fmt::Debug + Send + 'static> Component for Post ), visibility: Visibility::Public, - wide_layout: gtk::GridLayout::new(), + narrow_layout: gtk::BoxLayout::builder() + .orientation(gtk::Orientation::Vertical) + .spacing(5) + .build(), #[cfg(feature = "smart-summary")] smart_summary: crate::components::SmartSummaryButton::builder() @@ -406,59 +405,6 @@ impl<E: std::error::Error + std::fmt::Debug + Send + 'static> Component for Post model.visibility = post.visibility; } - let prev_layout = widgets.content.layout_manager().unwrap(); - let layout = &model.wide_layout; - widgets.content.set_layout_manager(Some(layout.clone())); - layout.set_column_homogeneous(false); - layout.set_row_spacing(10); - - enum Row<'a> { - TwoColumn(&'a gtk::Label, &'a gtk::Widget), - Span(&'a gtk::Widget), - SecondColumn(&'a gtk::Widget) - } - - for (row, content) in [ - Row::TwoColumn(&widgets.name_label, widgets.name_field.upcast_ref::<gtk::Widget>()), - Row::TwoColumn(&widgets.summary_label, widgets.summary_field.upcast_ref::<gtk::Widget>()), - Row::TwoColumn(&widgets.tag_label, widgets.tag_holder.upcast_ref::<gtk::Widget>()), - Row::SecondColumn(model.tags.widget().upcast_ref::<gtk::Widget>()), - Row::TwoColumn(&widgets.content_label, widgets.content_textarea_wrapper.upcast_ref::<gtk::Widget>()), - Row::Span(widgets.misc_prop_wrapper.upcast_ref::<gtk::Widget>()), - ].into_iter().enumerate() { - match content { - Row::TwoColumn(label, field) => { - let label_layout = layout.layout_child(label) - .downcast::<GridLayoutChild>() - .unwrap(); - label_layout.set_row(row as i32); - label_layout.set_column(0); - - let field_layout = layout.layout_child(field) - .downcast::<GridLayoutChild>() - .unwrap(); - field_layout.set_row(row as i32); - field_layout.set_column(1); - }, - Row::Span(widget) => { - let widget_layout = layout.layout_child(widget) - .downcast::<GridLayoutChild>() - .unwrap(); - widget_layout.set_row(row as i32); - widget_layout.set_column_span(2); - }, - Row::SecondColumn(widget) => { - let widget_layout = layout.layout_child(widget) - .downcast::<GridLayoutChild>() - .unwrap(); - widget_layout.set_row(row as i32); - widget_layout.set_column(1); - } - } - } - - widgets.content.set_layout_manager(Some(prev_layout)); - ComponentParts { model, widgets } } |