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

Remember window size and maximized state. #6

Merged
merged 2 commits into from
Sep 30, 2024
Merged
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
415 changes: 404 additions & 11 deletions Cargo.lock

Large diffs are not rendered by default.

557 changes: 556 additions & 1 deletion build-aux/cargo-sources.json

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions gnome/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,8 @@ gtk = { version = "0.9.0", package = "gtk4", features = ["blueprint", "v4_14"] }
flume = "0.11.0"
gettext-rs = { version = "0.7.1", features = ["gettext-system"] }
gtk-blueprint = "0.2.0"

[dependencies.cosmic-config]
git = "https://github.com/pop-os/libcosmic.git"
rev = "228eb4d70d581be88bacb1e261106a58603d847b"
default-features = false
67 changes: 67 additions & 0 deletions gnome/src/config/imp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use cosmic_config::{ConfigGet, ConfigSet};
use glib::prelude::*;
use gtk::{glib, subclass::prelude::*};
use std::cell::{Cell, OnceCell};

use crate::APP_ID;

#[derive(Default, glib::Properties)]
#[properties(wrapper_type = super::Config)]
pub struct Config {
#[property(get, set = Self::set_window_width, default = 1600)]
window_width: Cell<i32>,
#[property(get, set = Self::set_window_height, default = 900)]
window_height: Cell<i32>,
#[property(get, set = Self::set_window_maximized, default = false)]
window_maximized: Cell<bool>,

config: OnceCell<cosmic_config::Config>,
}

impl Config {
fn set_window_width(&self, width: i32) {
let config = self.config.get().unwrap();
let _ = config.set("window_width", width);
self.window_width.set(width);
}

fn set_window_height(&self, height: i32) {
let config = self.config.get().unwrap();
let _ = config.set("window_height", height);
self.window_height.set(height);
}

fn set_window_maximized(&self, maximized: bool) {
let config = self.config.get().unwrap();
let _ = config.set("window_maximized", maximized);
self.window_maximized.set(maximized);
}
}

// Basic declaration of our type for the GObject type system
#[glib::object_subclass]
impl ObjectSubclass for Config {
const NAME: &'static str = "ClapgrepConfig";
type Type = super::Config;
}

#[glib::derived_properties]
impl ObjectImpl for Config {
fn constructed(&self) {
let config = cosmic_config::Config::new(APP_ID, 1).expect("failed to open config");

if let Ok(window_width) = config.get("window_width") {
self.window_width.set(window_width);
}

if let Ok(window_height) = config.get("window_height") {
self.window_height.set(window_height);
}

if let Ok(window_maximized) = config.get("window_maximized") {
self.window_maximized.set(window_maximized);
}

let _ = self.config.set(config);
}
}
19 changes: 19 additions & 0 deletions gnome/src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
mod imp;

use gtk::glib;

glib::wrapper! {
pub struct Config(ObjectSubclass<imp::Config>);
}

impl Config {
pub fn new() -> Config {
glib::Object::new()
}
}

impl Default for Config {
fn default() -> Self {
Self::new()
}
}
7 changes: 5 additions & 2 deletions gnome/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ use gtk::{gio::SimpleAction, License};
use gtk_blueprint::include_blp;
use std::path::PathBuf;

mod config;
mod error_window;
mod search_match;
mod search_model;
mod search_result;
mod search_window;

const APP_ID: &str = "de.leopoldluley.Clapgrep";

fn setup_gettext() {
let mut text_domain = gettextrs::TextDomain::new("de.leopoldluley.Clapgrep");
let mut text_domain = gettextrs::TextDomain::new(APP_ID);

if cfg!(debug_assertions) {
let assets_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
Expand All @@ -30,7 +33,7 @@ fn main() {
setup_gettext();

let app = adw::Application::builder()
.application_id("de.leopoldluley.Clapgrep")
.application_id(APP_ID)
.flags(ApplicationFlags::HANDLES_OPEN)
.build();

Expand Down
24 changes: 21 additions & 3 deletions gnome/src/search_window/imp.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::{config::Config, error_window::ErrorWindow, search_model::SearchModel};
use adw::subclass::prelude::*;
use clapgrep_core::{
extended::ExtendedType,
Expand All @@ -17,8 +18,6 @@ use std::{
path::{Path, PathBuf},
};

use crate::{error_window::ErrorWindow, search_model::SearchModel};

#[derive(CompositeTemplate, glib::Properties, Default)]
#[template(file = "src/search_window/search_window.blp")]
#[properties(wrapper_type = super::SearchWindow)]
Expand Down Expand Up @@ -61,6 +60,7 @@ pub struct SearchWindow {
pub has_errors: Cell<bool>,

pub manager: RefCell<Option<Manager>>,
pub config: Config,
}

#[glib::object_subclass]
Expand Down Expand Up @@ -222,8 +222,26 @@ impl SearchWindow {
impl ObjectImpl for SearchWindow {
fn constructed(&self) {
self.parent_constructed();

let obj = self.obj();

self.config
.bind_property("window_width", obj.as_ref(), "default_width")
.bidirectional()
.sync_create()
.build();

self.config
.bind_property("window_height", obj.as_ref(), "default_height")
.bidirectional()
.sync_create()
.build();

self.config
.bind_property("window_maximized", obj.as_ref(), "maximized")
.bidirectional()
.sync_create()
.build();

obj.results().connect_items_changed(clone!(
#[weak]
obj,
Expand Down
2 changes: 0 additions & 2 deletions gnome/src/search_window/search_window.blp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ using Gtk 4.0;
using Adw 1;

template $ClapgrepSearchWindow: Adw.ApplicationWindow {
default-width: 1600;
default-height: 900;
title: _("Clapgrep");

content: Adw.NavigationSplitView {
Expand Down
Loading