2023-04-19 20:02:01 +02:00
|
|
|
|
// Copyright (c) 2020-2023 Christopher Davis
|
|
|
|
|
|
// Copyright (c) 2022-2023 Sophie Herold
|
|
|
|
|
|
// Copyright (c) 2022 Sabri Ünal
|
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 16:09:48 -08:00
|
|
|
|
use crate::deps::*;
|
2023-04-11 00:02:28 +02:00
|
|
|
|
use crate::util::gettext::*;
|
2021-12-21 16:09:48 -08:00
|
|
|
|
|
2021-12-21 12:15:46 -08:00
|
|
|
|
use adw::prelude::*;
|
|
|
|
|
|
use adw::subclass::prelude::*;
|
2020-12-26 02:05:34 -08:00
|
|
|
|
|
|
|
|
|
|
use crate::config;
|
2021-12-21 10:55:02 -08:00
|
|
|
|
use crate::window::LpWindow;
|
2020-12-26 02:05:34 -08:00
|
|
|
|
|
|
|
|
|
|
mod imp {
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
|
|
|
|
// The basic struct that holds our
|
|
|
|
|
|
// state and widgets
|
2022-02-04 15:42:11 -08:00
|
|
|
|
#[derive(Default, Debug)]
|
|
|
|
|
|
pub struct LpApplication {}
|
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 {
|
2022-11-02 21:11:43 +01:00
|
|
|
|
fn activate(&self) {
|
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) {
|
2023-02-12 02:07:42 +01:00
|
|
|
|
let application = self.obj();
|
2020-12-26 02:05:34 -08:00
|
|
|
|
for file in files {
|
2022-11-02 21:11:43 +01:00
|
|
|
|
let win = LpWindow::new(&*application);
|
2023-04-18 01:52:06 +02:00
|
|
|
|
win.image_view().set_image_from_file(file.clone());
|
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")
|
|
|
|
|
|
.activate(|app: &Self, _, _| app.show_about())
|
|
|
|
|
|
.build(),
|
|
|
|
|
|
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
|
|
|
|
|
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-01-31 21:56:44 +01:00
|
|
|
|
self.set_accels_for_action(
|
|
|
|
|
|
"win.trash",
|
|
|
|
|
|
&["<Ctrl>X", "Delete", "KP_Delete", "BackSpace"],
|
|
|
|
|
|
);
|
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"]);
|
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(
|
|
|
|
|
|
"win.zoom-in",
|
|
|
|
|
|
&[
|
|
|
|
|
|
"<Ctrl>plus",
|
|
|
|
|
|
"plus",
|
|
|
|
|
|
"<Ctrl>equal",
|
|
|
|
|
|
"equal",
|
|
|
|
|
|
"<Ctrl>KP_Add",
|
|
|
|
|
|
"KP_Add",
|
|
|
|
|
|
],
|
|
|
|
|
|
);
|
|
|
|
|
|
self.set_accels_for_action(
|
|
|
|
|
|
"win.zoom-out",
|
|
|
|
|
|
&["<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"]);
|
|
|
|
|
|
|
2022-11-23 19:12:19 +01:00
|
|
|
|
self.set_accels_for_action("win.rotate(90.0)", &["<Ctrl>R"]);
|
|
|
|
|
|
self.set_accels_for_action("win.rotate(-90.0)", &["<Ctrl><Shift>R"]);
|
|
|
|
|
|
|
|
|
|
|
|
self.set_accels_for_action("window.close", &["<Ctrl>W"]);
|
2020-12-26 02:05:34 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn show_about(&self) {
|
|
|
|
|
|
// Builders are a pattern that allow you to create
|
|
|
|
|
|
// an object and set all relevant properties very
|
|
|
|
|
|
// easily in a way that's idiomatic to Rust.
|
2022-08-06 00:49:59 -04:00
|
|
|
|
let about = adw::AboutWindow::builder()
|
2023-04-11 00:02:28 +02:00
|
|
|
|
.application_name(gettext("Loupe"))
|
2022-08-06 00:49:59 -04:00
|
|
|
|
.application_icon(config::APP_ID)
|
|
|
|
|
|
.version(config::VERSION)
|
2023-04-11 00:02:28 +02:00
|
|
|
|
.developer_name(gettext("The Loupe Team"))
|
2023-02-02 06:26:52 +01:00
|
|
|
|
.website("https://gitlab.gnome.org/Incubator/loupe")
|
|
|
|
|
|
.issue_url("https://gitlab.gnome.org/Incubator/loupe/-/issues/new")
|
2023-03-10 18:21:08 -05:00
|
|
|
|
.developers([
|
|
|
|
|
|
"Christopher Davis <christopherdavis@gnome.org>",
|
|
|
|
|
|
"Sophie Herold <sophieherold@gnome.org>",
|
2020-12-26 02:05:34 -08:00
|
|
|
|
])
|
2023-03-10 18:21:08 -05:00
|
|
|
|
.designers(["Allan Day", "Jakub Steiner", "Tobias Bernard"])
|
|
|
|
|
|
// Translators: Replace "translator-credits" with your names, one name per line
|
2023-04-11 00:02:28 +02:00
|
|
|
|
.translator_credits(gettext("translator-credits"))
|
|
|
|
|
|
.copyright(gettext("Copyright © 2020–2023 Christopher Davis et al."))
|
2020-12-26 02:05:34 -08:00
|
|
|
|
.license_type(gtk::License::Gpl30)
|
|
|
|
|
|
.build();
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
}
|