From d185973815af22397cc292067dc7fd1dc34a5d9c Mon Sep 17 00:00:00 2001 From: Sophie Herold Date: Wed, 20 Dec 2023 20:26:32 +0100 Subject: [PATCH] windowTitle: Add custom window title widget Fixes #202 --- src/widgets/mod.rs | 2 + src/widgets/window_title.rs | 97 +++++++++++++++++++++++++++++++++++++ src/window.rs | 3 ++ src/window.ui | 6 ++- 4 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 src/widgets/window_title.rs diff --git a/src/widgets/mod.rs b/src/widgets/mod.rs index 0379bed..4f3d013 100644 --- a/src/widgets/mod.rs +++ b/src/widgets/mod.rs @@ -25,6 +25,7 @@ mod print; mod print_preview; mod properties_view; mod sliding_view; +mod window_title; pub use drag_overlay::LpDragOverlay; pub use image::LpImage; @@ -34,3 +35,4 @@ pub use print::LpPrint; pub use print_preview::LpPrintPreview; pub use properties_view::LpPropertiesView; pub use sliding_view::LpSlidingView; +pub use window_title::LpWindowTitle; diff --git a/src/widgets/window_title.rs b/src/widgets/window_title.rs new file mode 100644 index 0000000..b55f7fa --- /dev/null +++ b/src/widgets/window_title.rs @@ -0,0 +1,97 @@ +// Copyright (c) 2023 Sophie Herold +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// +// SPDX-License-Identifier: GPL-3.0-or-later + +//! A window title +//! +//! This window title does not claim its complete width as natural width. This +//! avoids the window to grow to a large file name title instead of just +//! fittingly for the image size. + +use adw::prelude::*; +use adw::subclass::prelude::*; + +use crate::deps::*; + +mod imp { + use super::*; + + #[derive(Debug, Default, glib::Properties)] + #[properties(wrapper_type = super::LpWindowTitle)] + pub struct LpWindowTitle { + #[property(type = String, set = Self::set_title)] + _title: (), + window_title: adw::WindowTitle, + } + + #[glib::object_subclass] + impl ObjectSubclass for LpWindowTitle { + const NAME: &'static str = "LpWindowTitle"; + type Type = super::LpWindowTitle; + type ParentType = adw::Bin; + } + + impl ObjectImpl for LpWindowTitle { + fn constructed(&self) { + self.parent_constructed(); + let obj = self.obj(); + + obj.set_layout_manager(None::); + obj.set_child(Some(&self.window_title)); + obj.set_hexpand(true); + } + + fn properties() -> &'static [glib::ParamSpec] { + Self::derived_properties() + } + + fn set_property(&self, id: usize, value: &glib::Value, pspec: &glib::ParamSpec) { + self.derived_set_property(id, value, pspec) + } + + fn property(&self, id: usize, pspec: &glib::ParamSpec) -> glib::Value { + self.derived_property(id, pspec) + } + } + impl WidgetImpl for LpWindowTitle { + fn measure(&self, orientation: gtk::Orientation, for_size: i32) -> (i32, i32, i32, i32) { + let mut measure = self.window_title.measure(orientation, for_size); + + if orientation == gtk::Orientation::Horizontal { + // Set natural size to minimal size + measure.1 = measure.0; + } + + measure + } + + fn size_allocate(&self, width: i32, height: i32, baseline: i32) { + self.window_title.allocate(width, height, baseline, None); + } + } + impl BinImpl for LpWindowTitle {} + + impl LpWindowTitle { + fn set_title(&self, title: String) { + self.window_title.set_title(&title); + } + } +} + +glib::wrapper! { + pub struct LpWindowTitle(ObjectSubclass) + @extends gtk::Widget, adw::Bin; +} diff --git a/src/window.rs b/src/window.rs index a9d7369..bc19872 100644 --- a/src/window.rs +++ b/src/window.rs @@ -52,6 +52,7 @@ const HIDE_CONTROLS_IDLE_TIMEOUT: u64 = 3000; mod imp { use super::*; + use crate::widgets::LpWindowTitle; // To use composite templates, you need // to use derive macro. Derive macros generate @@ -78,6 +79,8 @@ mod imp { #[template_child] pub(super) headerbar: TemplateChild, #[template_child] + pub(super) title: TemplateChild, + #[template_child] pub(super) headerbar_events: TemplateChild, #[template_child] pub(super) properties_button: TemplateChild, diff --git a/src/window.ui b/src/window.ui index bea835b..cdb2e25 100644 --- a/src/window.ui +++ b/src/window.ui @@ -61,7 +61,6 @@ 498 360 360 - Image Viewer max-width: 590sp @@ -74,6 +73,11 @@ + + + + +