Skip to content

Commit

Permalink
Merge pull request #25 from luleyleo/restructure
Browse files Browse the repository at this point in the history
Restructure gnome module
  • Loading branch information
luleyleo authored Nov 12, 2024
2 parents fbd3ee3 + 75336eb commit caecc4a
Show file tree
Hide file tree
Showing 23 changed files with 370 additions and 342 deletions.
65 changes: 65 additions & 0 deletions gnome/src/app.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use crate::ui;
use adw::prelude::*;
use gtk::{
gdk,
gio::{self, SimpleAction},
glib::{self, clone},
STYLE_PROVIDER_PRIORITY_APPLICATION,
};

pub fn start(app: &adw::Application, files: &[gio::File]) {
let app = app.downcast_ref::<adw::Application>().unwrap();

let style_provider = gtk::CssProvider::new();
style_provider.load_from_string(include_str!("styles.css"));
gtk::style_context_add_provider_for_display(
&gdk::Display::default().unwrap(),
&style_provider,
STYLE_PROVIDER_PRIORITY_APPLICATION,
);

let window = ui::SearchWindow::new(app);

if let Some(dir) = files.first() {
if let Some(path) = dir.path() {
if path.is_dir() {
window.set_search_path(path);
}
}
}

let about_action = SimpleAction::new("about", None);
about_action.connect_activate(clone!(
#[weak]
window,
move |_, _| ui::about_dialog().present(Some(&window))
));
app.add_action(&about_action);

let shortcuts_action = SimpleAction::new("shortcuts", None);
shortcuts_action.connect_activate(clone!(
#[weak]
window,
move |_, _| {
ui::show_shortcuts(&window);
}
));
app.add_action(&shortcuts_action);

let quit_action = SimpleAction::new("quit", None);
quit_action.connect_activate(clone!(
#[weak]
window,
move |_, _| {
window.close();
}
));
app.add_action(&quit_action);

app.set_accels_for_action("app.quit", &["<ctrl>q"]);
app.set_accels_for_action("app.shortcuts", &["<ctrl>h"]);
app.set_accels_for_action("win.start-search", &["<ctrl>Return"]);
app.set_accels_for_action("app.stop-search", &["<ctrl>s"]);

window.present();
}
111 changes: 111 additions & 0 deletions gnome/src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
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()
}
}

mod imp {
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>,

#[property(get, set = Self::set_search_pdf, default = false)]
search_pdf: Cell<bool>,
#[property(get, set = Self::set_search_office, default = false)]
search_office: Cell<bool>,

config: OnceCell<cosmic_config::Config>,
}

#[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);
}

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

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

let _ = self.config.set(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);
}

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

fn set_search_office(&self, search_office: bool) {
let config = self.config.get().unwrap();
let _ = config.set("search_office", search_office);
self.search_office.set(search_office);
}
}
}
92 changes: 0 additions & 92 deletions gnome/src/config/imp.rs

This file was deleted.

19 changes: 0 additions & 19 deletions gnome/src/config/mod.rs

This file was deleted.

87 changes: 8 additions & 79 deletions gnome/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
use adw::prelude::*;
use gtk::gio::SimpleAction;
use gtk::gio::{self, ApplicationFlags};
use gtk::glib::{self, clone};
use gtk::{gdk, STYLE_PROVIDER_PRIORITY_APPLICATION};
use gtk_blueprint::include_blp;
use gtk::gio::ApplicationFlags;
use std::path::PathBuf;

mod about;
mod app;
mod config;
mod error_window;
mod search_match;
mod search_model;
mod search_result;
mod search_window;
mod search;
mod ui;

const APP_ID: &str = env!("APP_ID");

Expand All @@ -34,77 +27,13 @@ fn setup_gettext() {
fn main() {
setup_gettext();

let app = adw::Application::builder()
let application = adw::Application::builder()
.application_id(APP_ID)
.flags(ApplicationFlags::HANDLES_OPEN)
.build();

app.connect_open(|app, files, _| start(app, files));
app.connect_activate(|app| start(app, &[]));
application.connect_open(|a, files, _| app::start(a, files));
application.connect_activate(|a| app::start(a, &[]));

app.run();
}

fn start(app: &adw::Application, files: &[gio::File]) {
let app = app.downcast_ref::<adw::Application>().unwrap();

let style_provider = gtk::CssProvider::new();
style_provider.load_from_string(include_str!("styles.css"));
gtk::style_context_add_provider_for_display(
&gdk::Display::default().unwrap(),
&style_provider,
STYLE_PROVIDER_PRIORITY_APPLICATION,
);

let window = search_window::SearchWindow::new(app);

if let Some(dir) = files.first() {
if let Some(path) = dir.path() {
if path.is_dir() {
window.set_search_path(path);
}
}
}

let about_action = SimpleAction::new("about", None);
about_action.connect_activate(clone!(
#[weak]
window,
move |_, _| about::dialog().present(Some(&window))
));
app.add_action(&about_action);

let shortcuts_action = SimpleAction::new("shortcuts", None);
shortcuts_action.connect_activate(clone!(
#[weak]
window,
move |_, _| {
let blueprint = include_blp!("gnome/src/shortcuts.blp");
let builder = gtk::Builder::from_string(blueprint);
let help_overlay = builder
.object::<gtk::ShortcutsWindow>("help-overlay")
.unwrap();
help_overlay.set_transient_for(Some(&window));
help_overlay.set_application(window.application().as_ref());
help_overlay.present();
}
));
app.add_action(&shortcuts_action);

let quit_action = SimpleAction::new("quit", None);
quit_action.connect_activate(clone!(
#[weak]
window,
move |_, _| {
window.close();
}
));
app.add_action(&quit_action);

app.set_accels_for_action("app.quit", &["<ctrl>q"]);
app.set_accels_for_action("app.shortcuts", &["<ctrl>h"]);
app.set_accels_for_action("win.start-search", &["<ctrl>Return"]);
app.set_accels_for_action("app.stop-search", &["<ctrl>s"]);

window.present();
application.run();
}
Loading

0 comments on commit caecc4a

Please sign in to comment.