2023-04-19 20:02:01 +02:00
|
|
|
// Copyright (c) 2020-2023 Christopher Davis
|
2024-01-08 17:41:19 +01:00
|
|
|
// Copyright (c) 2022-2024 Sophie Herold
|
2023-04-26 18:18:09 +02:00
|
|
|
// Copyright (c) 2022-2023 Sabri Ünal
|
2023-12-06 23:45:02 +01:00
|
|
|
// Copyright (c) 2023 Matteo Nardi
|
2023-11-29 21:15:36 +01:00
|
|
|
// Copyright (c) 2023 Julian Hofer
|
2023-06-07 23:44:20 +02:00
|
|
|
// Copyright (c) 2023 Huan Nguyen
|
2020-12-26 02:05:34 -08:00
|
|
|
//
|
|
|
|
|
// 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
|
|
|
|
|
|
2021-12-21 12:15:46 -08:00
|
|
|
use adw::prelude::*;
|
|
|
|
|
use adw::subclass::prelude::*;
|
2020-12-26 02:05:34 -08:00
|
|
|
|
2023-12-06 23:45:02 +01:00
|
|
|
use crate::deps::*;
|
2021-12-21 10:55:02 -08:00
|
|
|
use crate::window::LpWindow;
|
2023-12-06 23:45:02 +01:00
|
|
|
use crate::{about, config};
|
2020-12-26 02:05:34 -08:00
|
|
|
|
|
|
|
|
mod imp {
|
|
|
|
|
use super::*;
|
2024-01-09 15:56:23 +01:00
|
|
|
use crate::widgets::LpShyBin;
|
2020-12-26 02:05:34 -08:00
|
|
|
|
|
|
|
|
// The basic struct that holds our
|
|
|
|
|
// state and widgets
|
2023-12-05 17:24:53 +01:00
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub struct LpApplication {
|
|
|
|
|
pub settings: gio::Settings,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Default for LpApplication {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
settings: gio::Settings::new(config::APP_ID),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-26 02:05:34 -08:00
|
|
|
|
|
|
|
|
// Sets up the basics for the GObject
|
2021-06-27 15:28:35 -07:00
|
|
|
// The `#[glib::object_subclass] macro implements
|
|
|
|
|
// some boilerplate code for the object setup, e.g. get_type()
|
|
|
|
|
#[glib::object_subclass]
|
2021-12-21 10:55:02 -08:00
|
|
|
impl ObjectSubclass for LpApplication {
|
|
|
|
|
const NAME: &'static str = "LpApplication";
|
|
|
|
|
type Type = super::LpApplication;
|
2021-12-21 12:15:46 -08:00
|
|
|
type ParentType = adw::Application;
|
2020-12-26 02:05:34 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Overrides GObject vfuncs
|
2021-12-21 10:55:02 -08:00
|
|
|
impl ObjectImpl for LpApplication {
|
2022-11-02 21:11:43 +01:00
|
|
|
fn constructed(&self) {
|
2023-02-12 02:07:42 +01:00
|
|
|
let obj = self.obj();
|
2022-11-02 21:11:43 +01:00
|
|
|
|
|
|
|
|
self.parent_constructed();
|
2020-12-26 02:05:34 -08:00
|
|
|
|
2021-09-23 02:51:46 -07:00
|
|
|
// Force dark theme
|
2021-09-28 17:56:33 -07:00
|
|
|
obj.style_manager()
|
2021-12-21 12:15:46 -08:00
|
|
|
.set_color_scheme(adw::ColorScheme::PreferDark);
|
2020-12-26 02:05:34 -08:00
|
|
|
|
|
|
|
|
// Set up the actions
|
|
|
|
|
obj.setup_actions();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Overrides GApplication vfuncs
|
2021-12-21 10:55:02 -08:00
|
|
|
impl ApplicationImpl for LpApplication {
|
2024-01-09 15:56:23 +01:00
|
|
|
fn startup(&self) {
|
2024-02-08 00:32:09 +01:00
|
|
|
log::trace!("Startup");
|
2024-01-09 15:56:23 +01:00
|
|
|
self.parent_startup();
|
|
|
|
|
LpShyBin::ensure_type();
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-02 21:11:43 +01:00
|
|
|
fn activate(&self) {
|
2024-02-08 00:32:09 +01:00
|
|
|
log::trace!("Activate");
|
2023-02-12 02:07:42 +01:00
|
|
|
let application = self.obj();
|
2022-11-02 21:11:43 +01:00
|
|
|
let window = LpWindow::new(&*application);
|
2020-12-26 02:05:34 -08:00
|
|
|
window.present();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Handles opening files from the command line or other applications
|
2022-11-02 21:11:43 +01:00
|
|
|
fn open(&self, files: &[gio::File], _hint: &str) {
|
2024-02-08 00:32:09 +01:00
|
|
|
log::trace!("Open {} file(s)", files.len());
|
2023-02-12 02:07:42 +01:00
|
|
|
let application = self.obj();
|
2023-05-30 12:52:59 +00:00
|
|
|
let win = LpWindow::new(&*application);
|
|
|
|
|
win.image_view().set_images_from_files(files.to_vec());
|
2020-12-26 02:05:34 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This is empty, but we still need to provide an
|
|
|
|
|
// empty implementation for each type we subclass.
|
2021-12-21 10:55:02 -08:00
|
|
|
impl GtkApplicationImpl for LpApplication {}
|
|
|
|
|
impl AdwApplicationImpl for LpApplication {}
|
2020-12-26 02:05:34 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Creates a wrapper struct that inherits the functions
|
|
|
|
|
// from objects listed as @extends or interfaces it @implements.
|
|
|
|
|
// This is what allows us to do e.g. application.quit() on
|
2021-12-21 10:55:02 -08:00
|
|
|
// LpApplication without casting.
|
2020-12-26 02:05:34 -08:00
|
|
|
glib::wrapper! {
|
2021-12-21 10:55:02 -08:00
|
|
|
pub struct LpApplication(ObjectSubclass<imp::LpApplication>)
|
2021-12-21 12:15:46 -08:00
|
|
|
@extends gio::Application, gtk::Application, adw::Application,
|
2020-12-26 02:05:34 -08:00
|
|
|
@implements gio::ActionGroup, gio::ActionMap;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-21 10:55:02 -08:00
|
|
|
// This is where the member functions of LpApplication go.
|
2021-09-22 22:57:54 -07:00
|
|
|
#[allow(clippy::new_without_default)]
|
2021-12-21 10:55:02 -08:00
|
|
|
impl LpApplication {
|
2020-12-26 02:05:34 -08:00
|
|
|
pub fn new() -> Self {
|
2022-11-24 12:26:04 -05:00
|
|
|
glib::Object::builder()
|
|
|
|
|
.property("application-id", config::APP_ID)
|
|
|
|
|
.property("flags", gio::ApplicationFlags::HANDLES_OPEN)
|
|
|
|
|
.property("resource-base-path", "/org/gnome/Loupe")
|
|
|
|
|
.build()
|
2020-12-26 02:05:34 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn setup_actions(&self) {
|
2022-11-23 13:51:27 -05:00
|
|
|
// gio::ActionEntryBuilder allows us to build and store an action on an object
|
|
|
|
|
// that implements gio::ActionMap. Here we build the application's actions and
|
|
|
|
|
// add them with add_action_entries().
|
|
|
|
|
let actions = [
|
|
|
|
|
gio::ActionEntryBuilder::new("about")
|
2023-07-08 22:33:44 +02:00
|
|
|
.activate(|app: &Self, _, _| {
|
|
|
|
|
let app = app.clone();
|
2023-11-22 09:12:07 +01:00
|
|
|
glib::spawn_future_local(async move { app.show_about().await });
|
2023-07-08 22:33:44 +02:00
|
|
|
})
|
2022-11-23 13:51:27 -05:00
|
|
|
.build(),
|
2023-06-21 09:15:37 -04:00
|
|
|
gio::ActionEntryBuilder::new("help")
|
|
|
|
|
.activate(|app: &Self, _, _| app.show_help())
|
|
|
|
|
.build(),
|
2022-11-23 13:51:27 -05:00
|
|
|
gio::ActionEntryBuilder::new("quit")
|
|
|
|
|
.activate(|app: &Self, _, _| app.quit())
|
|
|
|
|
.build(),
|
|
|
|
|
gio::ActionEntryBuilder::new("new-window")
|
|
|
|
|
.activate(|app: &Self, _, _| {
|
|
|
|
|
let win = LpWindow::new(app);
|
2023-01-14 14:43:46 -05:00
|
|
|
win.present();
|
2022-11-23 13:51:27 -05:00
|
|
|
})
|
|
|
|
|
.build(),
|
|
|
|
|
];
|
|
|
|
|
|
2023-02-12 02:07:42 +01:00
|
|
|
self.add_action_entries(actions);
|
2020-12-26 02:05:34 -08:00
|
|
|
|
2023-06-21 09:15:37 -04:00
|
|
|
self.set_accels_for_action("app.help", &["F1"]);
|
2022-11-23 19:12:19 +01:00
|
|
|
self.set_accels_for_action("app.quit", &["<Ctrl>Q"]);
|
|
|
|
|
self.set_accels_for_action("app.new-window", &["<Ctrl>N"]);
|
|
|
|
|
|
|
|
|
|
self.set_accels_for_action("win.open", &["<Ctrl>O"]);
|
|
|
|
|
self.set_accels_for_action("win.print", &["<Ctrl>P"]);
|
|
|
|
|
self.set_accels_for_action("win.copy", &["<Ctrl>C"]);
|
2023-05-31 08:24:44 -04:00
|
|
|
self.set_accels_for_action("win.trash", &["Delete", "KP_Delete"]);
|
2022-11-23 19:12:19 +01:00
|
|
|
self.set_accels_for_action("win.show-help-overlay", &["<Ctrl>question"]);
|
|
|
|
|
self.set_accels_for_action("win.leave-fullscreen", &["Escape"]);
|
2022-12-17 17:07:22 +00:00
|
|
|
self.set_accels_for_action("win.toggle-properties", &["F9", "<Alt>Return"]);
|
2020-12-26 02:05:34 -08:00
|
|
|
self.set_accels_for_action("win.toggle-fullscreen", &["F11"]);
|
2022-12-17 17:07:22 +00:00
|
|
|
self.set_accels_for_action("win.set-background", &["<Ctrl>F8"]);
|
2022-11-23 19:12:19 +01:00
|
|
|
|
2023-02-06 21:07:23 +01:00
|
|
|
self.set_accels_for_action("win.image-left", &["Left", "Page_Down"]);
|
|
|
|
|
self.set_accels_for_action("win.image-right", &["Right", "Page_Up"]);
|
2024-01-31 23:34:25 +01:00
|
|
|
self.set_accels_for_action("win.image-left-instant", &["<Alt>Left"]);
|
|
|
|
|
self.set_accels_for_action("win.image-right-instant", &["<Alt>Right"]);
|
2023-02-06 19:14:01 +01:00
|
|
|
self.set_accels_for_action("win.first", &["Home"]);
|
|
|
|
|
self.set_accels_for_action("win.last", &["End"]);
|
2022-12-06 18:24:32 +01:00
|
|
|
|
2023-04-13 04:52:14 +02:00
|
|
|
self.set_accels_for_action(
|
|
|
|
|
"win.zoom-to-exact(1.0)",
|
|
|
|
|
&["1", "KP_1", "<Ctrl>1", "<Ctrl>KP_1"],
|
|
|
|
|
);
|
|
|
|
|
self.set_accels_for_action(
|
|
|
|
|
"win.zoom-to-exact(2.0)",
|
|
|
|
|
&["2", "KP_2", "<Ctrl>2", "<Ctrl>KP_2"],
|
|
|
|
|
);
|
2022-12-17 17:07:22 +00:00
|
|
|
self.set_accels_for_action("win.zoom-best-fit", &["0", "KP_0", "<Ctrl>0", "<Ctrl>KP_0"]);
|
2022-11-23 19:12:19 +01:00
|
|
|
self.set_accels_for_action(
|
2024-01-04 14:33:23 +01:00
|
|
|
"win.zoom-in-cursor",
|
2022-11-23 19:12:19 +01:00
|
|
|
&[
|
|
|
|
|
"<Ctrl>plus",
|
|
|
|
|
"plus",
|
|
|
|
|
"<Ctrl>equal",
|
|
|
|
|
"equal",
|
|
|
|
|
"<Ctrl>KP_Add",
|
|
|
|
|
"KP_Add",
|
|
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
self.set_accels_for_action(
|
2024-01-04 14:33:23 +01:00
|
|
|
"win.zoom-out-cursor",
|
2022-11-23 19:12:19 +01:00
|
|
|
&["<Ctrl>minus", "minus", "<Ctrl>KP_Subtract", "KP_Subtract"],
|
|
|
|
|
);
|
2023-01-30 19:07:17 +01:00
|
|
|
|
|
|
|
|
self.set_accels_for_action("win.pan-left", &["<Ctrl>Left"]);
|
|
|
|
|
self.set_accels_for_action("win.pan-right", &["<Ctrl>Right"]);
|
|
|
|
|
self.set_accels_for_action("win.pan-up", &["<Ctrl>Up"]);
|
|
|
|
|
self.set_accels_for_action("win.pan-down", &["<Ctrl>Down"]);
|
|
|
|
|
|
2023-04-24 02:11:43 +03:00
|
|
|
self.set_accels_for_action("win.rotate_cw", &["<Ctrl>R"]);
|
|
|
|
|
self.set_accels_for_action("win.rotate_ccw", &["<Ctrl><Shift>R"]);
|
2022-11-23 19:12:19 +01:00
|
|
|
|
|
|
|
|
self.set_accels_for_action("window.close", &["<Ctrl>W"]);
|
2020-12-26 02:05:34 -08:00
|
|
|
}
|
|
|
|
|
|
2023-07-08 22:33:44 +02:00
|
|
|
pub async fn show_about(&self) {
|
|
|
|
|
let about = about::window().await;
|
2020-12-26 02:05:34 -08:00
|
|
|
|
2021-06-27 15:28:35 -07:00
|
|
|
if let Some(window) = self.active_window() {
|
2022-08-06 00:49:59 -04:00
|
|
|
about.set_transient_for(Some(&window));
|
2020-12-26 02:05:34 -08:00
|
|
|
}
|
|
|
|
|
|
2023-01-14 14:43:46 -05:00
|
|
|
about.present();
|
2020-12-26 02:05:34 -08:00
|
|
|
}
|
2023-06-21 09:15:37 -04:00
|
|
|
|
|
|
|
|
pub fn show_help(&self) {
|
2023-11-22 09:12:07 +01:00
|
|
|
let context = self
|
|
|
|
|
.active_window()
|
|
|
|
|
.map(|w| gtk::prelude::WidgetExt::display(&w).app_launch_context());
|
|
|
|
|
glib::spawn_future_local(async move {
|
|
|
|
|
if let Err(e) =
|
|
|
|
|
gio::AppInfo::launch_default_for_uri_future("help:loupe", context.as_ref()).await
|
|
|
|
|
{
|
2023-06-21 09:15:37 -04:00
|
|
|
log::error!("Failed to launch help: {}", e.message());
|
|
|
|
|
}
|
2023-11-22 09:12:07 +01:00
|
|
|
});
|
2023-06-21 09:15:37 -04:00
|
|
|
}
|
2023-12-05 17:24:53 +01:00
|
|
|
|
|
|
|
|
pub fn settings(&self) -> gio::Settings {
|
|
|
|
|
self.imp().settings.clone()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Default for LpApplication {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
gio::Application::default()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.downcast::<LpApplication>()
|
|
|
|
|
.unwrap()
|
|
|
|
|
}
|
2020-12-26 02:05:34 -08:00
|
|
|
}
|