windowTitle: Add custom window title widget

Fixes #202
This commit is contained in:
Sophie Herold 2023-12-20 20:26:32 +01:00
parent d39bd774d5
commit d185973815
4 changed files with 107 additions and 1 deletions

View file

@ -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;

View file

@ -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 <http://www.gnu.org/licenses/>.
//
// 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::<gtk::LayoutManager>);
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<imp::LpWindowTitle>)
@extends gtk::Widget, adw::Bin;
}

View file

@ -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<adw::HeaderBar>,
#[template_child]
pub(super) title: TemplateChild<LpWindowTitle>,
#[template_child]
pub(super) headerbar_events: TemplateChild<gtk::EventControllerMotion>,
#[template_child]
pub(super) properties_button: TemplateChild<gtk::ToggleButton>,

View file

@ -61,7 +61,6 @@
<property name="default-height">498</property>
<property name="width-request">360</property>
<property name="height-request">360</property>
<property name="title" translatable="yes">Image Viewer</property>
<child>
<object class="AdwBreakpoint">
<condition>max-width: 590sp</condition>
@ -74,6 +73,11 @@
<object class="AdwHeaderBar" id="headerbar">
<property name="show-end-title-buttons" bind-source="LpWindow" bind-property="fullscreened" bind-flags="sync-create|invert-boolean" />
<property name="show-start-title-buttons" bind-source="LpWindow" bind-property="fullscreened" bind-flags="sync-create|invert-boolean" />
<property name="title-widget">
<object class="LpWindowTitle" id="title">
<property name="title" bind-source="LpWindow" bind-property="title" />
</object>
</property>
<style>
<class name="fullscreen-osd" />
</style>