Skip to content

Commit

Permalink
move style switcher to Tile
Browse files Browse the repository at this point in the history
  • Loading branch information
vhdirk committed Jan 29, 2024
1 parent e141e96 commit 767e4cb
Show file tree
Hide file tree
Showing 15 changed files with 92 additions and 90 deletions.
6 changes: 3 additions & 3 deletions data/io.github.vhdirk.Terms.gschema.xml.in
Original file line number Diff line number Diff line change
Expand Up @@ -295,17 +295,17 @@
<default>['&lt;Shift&gt;&lt;Control&gt;question']</default>
<summary>WinShowHelpOverlay</summary>
</key>
<key name="win-zoom-in" type="as">
<key name="zoom-in" type="as">
<default>['&lt;Control&gt;plus']</default>
<summary>Zoom In</summary>
<description>The shortcut to zoom in</description>
</key>
<key name="win-zoom-out" type="as">
<key name="zoom-out" type="as">
<default>['&lt;Control&gt;minus']</default>
<summary>Zoom Out</summary>
<description>The shortcut to zoom out</description>
</key>
<key name="win-zoom-default" type="as">
<key name="zoom-reset" type="as">
<default>['&lt;ctrl&gt;0', '&lt;Shift&gt;&lt;Control&gt;parenright']</default>
<summary>Reset Zoom</summary>
<description>The shortcut to reset zoom</description>
Expand Down
5 changes: 4 additions & 1 deletion data/resources/resources.gresource.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
<file compressed="true" preprocess="xml-stripblanks" alias="terminal_panel.ui">../../src/components/terminal_panel/terminal_panel.ui</file>
<file compressed="true" preprocess="xml-stripblanks" alias="terminal_tab.ui">../../src/components/terminal_tab/terminal_tab.ui</file>
<file compressed="true" preprocess="xml-stripblanks" alias="search_toolbar.ui">../../src/components/search_toolbar/search_toolbar.ui</file>
<file compressed="true" preprocess="xml-stripblanks" alias="style_switcher.ui">../../src/components/style_switcher/style_switcher.ui</file>
</gresource>
<gresource prefix="/io/github/vhdirk/Terms/svg">
<file compressed="true" preprocess="xml-stripblanks" alias="theme-thumbnail.svg">svg/theme-thumbnail.svg</file>
</gresource>

<gresource prefix="/io/github/vhdirk/Terms/icons/scalable/actions">

<!-- TODO: move this to Tile -->
<file compressed="true" preprocess="xml-stripblanks" alias="checkmark-small-symbolic.svg">../icons/actions/checkmark-small-symbolic.svg</file>
</gresource>
<gresource prefix="/io/github/vhdirk/Terms/icons/scalable/apps">
Expand All @@ -32,5 +34,6 @@
</gresource>
<gresource prefix="/io/github/vhdirk/Tile/gtk">
<file compressed="true" preprocess="xml-stripblanks" alias="zoom_controls.ui">../../src/tile/zoom_controls/zoom_controls.ui</file>
<file compressed="true" preprocess="xml-stripblanks" alias="style_switcher.ui">../../src/tile/style_switcher/style_switcher.ui</file>
</gresource>
</gresources>
16 changes: 8 additions & 8 deletions data/resources/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ window:not(.about) headerbar,
padding: 12px;
}

#terms_style_switcher {
#tile_style_switcher {
padding: 6px;
}

#terms_style_switcher .check {
#tile_style_switcher .check {
background: @accent_bg_color;
color: @accent_fg_color;
padding: 2px;
Expand All @@ -97,12 +97,12 @@ window:not(.about) headerbar,

/* Adapted from https://gitlab.gnome.org/GNOME/gnome-text-editor/-/blob/bf8c0c249f06a0be69e65aed3b786ba02a9f999e/src/TextEditor.css#L51 */

#terms_style_switcher checkbutton {
#tile_style_switcher checkbutton {
outline-offset: 1px;
transition: none;
}

#terms_style_switcher checkbutton radio {
#tile_style_switcher checkbutton radio {
-gtk-icon-source: none;
background: none;
padding: 12px;
Expand All @@ -114,20 +114,20 @@ window:not(.about) headerbar,
box-shadow: inset 0 0 0 1px @borders;
}

#terms_style_switcher checkbutton radio:checked {
#tile_style_switcher checkbutton radio:checked {
box-shadow: inset 0 0 0 2px @accent_bg_color;
}

#terms_style_switcher checkbutton.system radio {
#tile_style_switcher checkbutton.system radio {
background: linear-gradient(-45deg, #1e1e1e 49.99%, white 50.01%);
}

#terms_style_switcher checkbutton.light radio {
#tile_style_switcher checkbutton.light radio {
color: alpha(black, 0.8);
background-color: white;
}

#terms_style_switcher checkbutton.dark radio {
#tile_style_switcher checkbutton.dark radio {
color: white;
background-color: #1e1e1e;
}
6 changes: 3 additions & 3 deletions data/resources/ui/help_overlay.ui
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,19 @@
<child>
<object class="GtkShortcutsShortcut">
<property name="title" translatable="yes" context="shortcut window">Zoom in</property>
<property name="action-name">win.zoom-in</property>
<property name="action-name">zoom.in</property>
</object>
</child>
<child>
<object class="GtkShortcutsShortcut">
<property name="title" translatable="yes" context="shortcut window">Zoom out</property>
<property name="action-name">win.zoom-out</property>
<property name="action-name">zoom.out</property>
</object>
</child>
<child>
<object class="GtkShortcutsShortcut">
<property name="title" translatable="yes" context="shortcut window">Reset zoom</property>
<property name="action-name">win.zoom-default</property>
<property name="action-name">zoom.reset</property>
</object>
</child>
</object>
Expand Down
14 changes: 11 additions & 3 deletions src/components/header_bar/header_bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ use std::cell::{Cell, RefCell};
use adw::subclass::prelude::*;
use glib::clone;
use glib::{self, Properties, StaticTypeExt};
use gtk::prelude::*;
use gtk::{prelude::*, template_callbacks};
use tracing::info;

use crate::components::StyleSwitcher;
use crate::settings::Settings;
use crate::settings::{Settings, StylePreference};
use crate::tile::StyleSwitcher;
use crate::tile::ZoomControls;

#[derive(Debug, Default, gtk::CompositeTemplate, Properties)]
Expand All @@ -34,12 +34,18 @@ pub struct HeaderBar {
#[template_child]
pub menu_button: TemplateChild<gtk::MenuButton>,

#[template_child]
pub style_switcher: TemplateChild<StyleSwitcher>,

#[property(get, set, nullable)]
pub title: RefCell<Option<String>>,

#[property(get, set)]
pub fullscreened: Cell<bool>,

#[property(get, set, construct, default = 100)]
pub zoom: Cell<u32>,

#[property(get, set, nullable)]
pub overlay: RefCell<Option<gtk::Overlay>>,

Expand Down Expand Up @@ -89,6 +95,8 @@ impl HeaderBar {
.sync_create()
.build();

self.settings.bind_style_preference(&*self.style_switcher, "preference").build();

self.settings.bind_show_headerbar(&*self.revealer, "reveal-child").get_only().build();

self.revealer.connect_child_revealed_notify(clone!(@weak self as this => move |r| {
Expand Down
3 changes: 2 additions & 1 deletion src/components/header_bar/header_bar.ui
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,11 @@
<object class="GtkPopoverMenu">
<property name="menu-model">main-menu</property>
<child type="style-switcher">
<object class="TermsStyleSwitcher" id="style_switcher"></object>
<object class="TileStyleSwitcher" id="style_switcher"></object>
</child>
<child type="zoom-controls">
<object class="TileZoomControls" id="zoom_controls">
<property name="value" bind-source="TermsHeaderBar" bind-property="zoom" bind-flags="sync-create" />
</object>
</child>
</object>
Expand Down
2 changes: 0 additions & 2 deletions src/components/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ mod search_toolbar;
mod shortcut_dialog;
mod shortcut_row;
mod shortcuts_preferences_page;
mod style_switcher;
mod terminal;
mod terminal_panel;
mod terminal_preferences_page;
Expand All @@ -20,7 +19,6 @@ pub use search_toolbar::*;
pub use shortcut_dialog::*;
pub use shortcut_row::*;
pub use shortcuts_preferences_page::*;
pub use style_switcher::*;
pub use terminal::*;
pub use terminal_panel::*;
pub use terminal_preferences_page::*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,19 +255,19 @@
<child>
<object class="TermsShortcutRow" id="zoom_in">
<property name="title" translatable="yes">Zoom In</property>
<property name="action">win.zoom-in</property>
<property name="action">zoom.in</property>
</object>
</child>
<child>
<object class="TermsShortcutRow" id="zoom_out">
<property name="title" translatable="yes">Zoom Out</property>
<property name="action">win.zoom-out</property>
<property name="action">zoom.out</property>
</object>
</child>
<child>
<object class="TermsShortcutRow" id="zoom_default">
<property name="title" translatable="yes">Reset Zoom</property>
<property name="action">win.zoom-default</property>
<property name="action">zoom.reset</property>
</object>
</child>
</object>
Expand Down
23 changes: 10 additions & 13 deletions src/components/window/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,15 @@ impl ObjectSubclass for Window {
klass.bind_template();
klass.bind_template_callbacks();

// klass.install_action("tab.close-others", None, move |win: &super::Window, _, _| {
// win.imp().close_other_tabs();
// });
klass.install_action("zoom.out", None, move |win: &super::Window, _, _| {
win.imp().zoom_out();
});
klass.install_action("zoom.reset", None, move |win: &super::Window, _, _| {
win.imp().zoom_reset();
});
klass.install_action("zoom.in", None, move |win: &super::Window, _, _| {
win.imp().zoom_in();
});
}

fn instance_init(obj: &glib::subclass::InitializingObject<Self>) {
Expand Down Expand Up @@ -212,15 +218,6 @@ impl Window {
gio::ActionEntry::builder("toggle-fullscreen")
.activate(move |win: &super::Window, _, _| win.set_fullscreened(!win.is_fullscreened()))
.build(),
gio::ActionEntry::builder("zoom-out")
.activate(move |win: &super::Window, _, _| win.imp().zoom_out())
.build(),
gio::ActionEntry::builder("zoom-default")
.activate(move |win: &super::Window, _, _| win.imp().zoom_default())
.build(),
gio::ActionEntry::builder("zoom-in")
.activate(move |win: &super::Window, _, _| win.imp().zoom_in())
.build(),
gio::ActionEntry::builder("move-tab-left")
.activate(move |win: &super::Window, _, _| win.imp().move_tab_left())
.build(),
Expand Down Expand Up @@ -305,7 +302,7 @@ impl Window {
warn!("Zoom out: not yet implemented");
}

fn zoom_default(&self) {
fn zoom_reset(&self) {
// TODO
warn!("Zoom default: not yet implemented");
}
Expand Down
3 changes: 3 additions & 0 deletions src/tile/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
mod zoom_controls;
pub use zoom_controls::*;

mod style_switcher;
pub use style_switcher::*;
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use glib::Properties;
use gtk::subclass::prelude::*;
use gtk::{prelude::*, CompositeTemplate};
/// This file is work derived from Black Box
///
/// Copyright 2023 Paulo Queiroz <[email protected]>
Expand All @@ -17,29 +20,27 @@
///
/// SPDX-License-Identifier: GPL-3.0-or-later
///
use glib::clone;
use gtk::subclass::prelude::*;
use gtk::{prelude::*, CompositeTemplate};
use std::cell::RefCell;
use tracing::*;

use crate::settings::{Settings, StylePreference};

#[derive(Debug, Default, CompositeTemplate)]
#[template(resource = "/io/github/vhdirk/Terms/gtk/style_switcher.ui")]
#[derive(Debug, Default, CompositeTemplate, Properties)]
#[template(resource = "/io/github/vhdirk/Tile/gtk/style_switcher.ui")]
#[properties(wrapper_type=super::StyleSwitcher)]
pub struct StyleSwitcher {
settings: Settings,

#[template_child]
pub system_selector: TemplateChild<gtk::CheckButton>,
#[template_child]
pub light_selector: TemplateChild<gtk::CheckButton>,
#[template_child]
pub dark_selector: TemplateChild<gtk::CheckButton>,

#[property(get, set=Self::set_style_preference ,construct, default="system")]
pub preference: RefCell<String>,
}

#[glib::object_subclass]
impl ObjectSubclass for StyleSwitcher {
const NAME: &'static str = "TermsStyleSwitcher";
const NAME: &'static str = "TileStyleSwitcher";
type Type = super::StyleSwitcher;
type ParentType = gtk::Widget;

Expand All @@ -53,63 +54,53 @@ impl ObjectSubclass for StyleSwitcher {
obj.init_template();
}
}

#[glib::derived_properties]
impl ObjectImpl for StyleSwitcher {
fn constructed(&self) {
self.parent_constructed();

self.settings
.connect_style_preference_changed(clone!(@weak self as this => move |_| this.on_style_changed()));
self.on_style_changed();
}
}

impl WidgetImpl for StyleSwitcher {}

#[gtk::template_callbacks]
impl StyleSwitcher {
fn on_style_changed(&self) {
info!("on style changed");
fn set_style_preference(&self, preference: &str) {
let _system_guard = self.system_selector.freeze_notify();
let _light_guard = self.light_selector.freeze_notify();
let _dark_guard = self.dark_selector.freeze_notify();

match self.settings.style_preference() {
StylePreference::System => {
match preference {
"system" => {
self.system_selector.set_active(true);
self.light_selector.set_active(false);
self.dark_selector.set_active(false);
},
StylePreference::Light => {
"light" => {
self.system_selector.set_active(false);
self.light_selector.set_active(true);
self.dark_selector.set_active(false);
},
StylePreference::Dark => {
"dark" => {
self.system_selector.set_active(false);
self.light_selector.set_active(false);
self.dark_selector.set_active(true);
},
_ => {
warn!("Invalid style preference: {:?}", preference)
},
}
}

#[template_callback]
fn theme_check_active_changed(&self) {
info!("theme_check_active_changed");

if self.system_selector.is_active() {
self.change_style_preference(StylePreference::System);
self.preference.set("system".to_string());
} else if self.light_selector.is_active() {
self.change_style_preference(StylePreference::Light);
self.preference.set("light".to_string());
} else if self.dark_selector.is_active() {
self.change_style_preference(StylePreference::Dark);
}
}

fn change_style_preference(&self, style_pref: StylePreference) {
if self.settings.style_preference() != style_pref {
info!("Setting style preference {:?}", style_pref);
self.settings.set_style_preference(style_pref);
self.preference.set("dark".to_string());
}
self.obj().notify_preference();
}
}
Loading

0 comments on commit 767e4cb

Please sign in to comment.