2020-12-26 02:05:34 -08:00
|
|
|
// application.rs
|
|
|
|
|
//
|
|
|
|
|
// Copyright 2020 Christopher Davis <christopherdavis@gnome.org>
|
|
|
|
|
//
|
|
|
|
|
// 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::*;
|
|
|
|
|
|
2021-12-21 12:15:46 -08:00
|
|
|
use adw::prelude::*;
|
|
|
|
|
use adw::subclass::prelude::*;
|
2020-12-26 02:05:34 -08:00
|
|
|
use glib::clone;
|
|
|
|
|
use gtk::subclass::prelude::*;
|
|
|
|
|
use gtk_macros::*;
|
|
|
|
|
|
|
|
|
|
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 {
|
2020-12-26 02:05:34 -08:00
|
|
|
fn constructed(&self, obj: &Self::Type) {
|
|
|
|
|
self.parent_constructed(obj);
|
|
|
|
|
|
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 {
|
2020-12-26 02:05:34 -08:00
|
|
|
fn activate(&self, application: &Self::Type) {
|
2021-12-21 10:55:02 -08: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
|
|
|
|
|
fn open(&self, application: &Self::Type, files: &[gio::File], _hint: &str) {
|
|
|
|
|
for file in files {
|
2021-12-21 10:55:02 -08:00
|
|
|
let win = LpWindow::new(application);
|
2021-12-27 15:03:31 -08:00
|
|
|
win.set_image_from_file(file, true);
|
2020-12-26 02:05:34 -08:00
|
|
|
win.show();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
|
glib::Object::new(&[
|
|
|
|
|
("application-id", &config::APP_ID.to_string()),
|
|
|
|
|
("flags", &gio::ApplicationFlags::HANDLES_OPEN),
|
2021-09-28 17:44:26 -07:00
|
|
|
("resource-base-path", &"/org/gnome/Loupe/".to_string()),
|
2020-12-26 02:05:34 -08:00
|
|
|
])
|
|
|
|
|
.unwrap()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn setup_actions(&self) {
|
|
|
|
|
// action! is a macro from gtk_macros
|
|
|
|
|
// that creates a GSimpleAction with a callback.
|
|
|
|
|
// clone! is a macro from glib-rs that allows
|
|
|
|
|
// you to easily handle references in callbacks
|
|
|
|
|
// without refcycles or leaks.
|
|
|
|
|
//
|
|
|
|
|
// When you don't want the callback to keep the
|
|
|
|
|
// Object alive, pass as @weak. Otherwise, pass
|
|
|
|
|
// as @strong. Most of the time you will want
|
|
|
|
|
// to use @weak.
|
|
|
|
|
action!(
|
|
|
|
|
self,
|
|
|
|
|
"about",
|
|
|
|
|
clone!(@weak self as app => move |_, _| {
|
|
|
|
|
app.show_about();
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
action!(
|
|
|
|
|
self,
|
|
|
|
|
"help",
|
|
|
|
|
clone!(@weak self as app => move |_, _| {
|
|
|
|
|
app.show_help();
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
action!(
|
|
|
|
|
self,
|
|
|
|
|
"quit",
|
|
|
|
|
clone!(@weak self as app => move |_, _| {
|
|
|
|
|
app.quit();
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
action!(
|
|
|
|
|
self,
|
|
|
|
|
"new-window",
|
|
|
|
|
clone!(@weak self as app => move |_, _| {
|
2021-12-21 10:55:02 -08:00
|
|
|
let win = LpWindow::new(&app);
|
2020-12-26 02:05:34 -08:00
|
|
|
win.show();
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
self.set_accels_for_action("app.help", &["F1"]);
|
|
|
|
|
self.set_accels_for_action("app.quit", &["<Primary>Q"]);
|
|
|
|
|
self.set_accels_for_action("app.new-window", &["<Primary>N"]);
|
|
|
|
|
self.set_accels_for_action("win.open", &["<Primary>O"]);
|
|
|
|
|
self.set_accels_for_action("win.print", &["<Primary>P"]);
|
2022-01-08 14:02:16 +01:00
|
|
|
self.set_accels_for_action("win.copy", &["<Primary>C"]);
|
2021-12-22 16:02:17 -08:00
|
|
|
self.set_accels_for_action("win.show-help-overlay", &["<Primary>question"]);
|
2020-12-26 02:05:34 -08:00
|
|
|
self.set_accels_for_action("win.toggle-fullscreen", &["F11"]);
|
|
|
|
|
self.set_accels_for_action("window.close", &["<Primary>W"]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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.
|
2021-12-22 18:17:38 -08:00
|
|
|
let dialog = gtk::AboutDialog::builder()
|
2020-12-26 02:05:34 -08:00
|
|
|
.authors(vec![String::from(
|
|
|
|
|
"Christopher Davis <christopherdavis@gnome.org>",
|
|
|
|
|
)])
|
|
|
|
|
.artists(vec![
|
|
|
|
|
String::from("Allan Day"),
|
|
|
|
|
String::from("Jakub Steiner"),
|
|
|
|
|
String::from("Tobias Bernard"),
|
|
|
|
|
])
|
2021-09-29 00:36:23 -07:00
|
|
|
.copyright("© 2021 The GNOME Project")
|
2020-12-26 02:05:34 -08:00
|
|
|
.license_type(gtk::License::Gpl30)
|
|
|
|
|
.program_name("Image Viewer")
|
|
|
|
|
.logo_icon_name(config::APP_ID)
|
|
|
|
|
.version(config::VERSION)
|
|
|
|
|
.build();
|
|
|
|
|
|
2021-06-27 15:28:35 -07:00
|
|
|
if let Some(window) = self.active_window() {
|
2020-12-26 02:05:34 -08:00
|
|
|
dialog.set_modal(true);
|
|
|
|
|
dialog.set_transient_for(Some(&window));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dialog.show();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn show_help(&self) {
|
2021-09-28 17:44:26 -07:00
|
|
|
gtk::show_uri(self.active_window().as_ref(), "help:loupe", 0);
|
2020-12-26 02:05:34 -08:00
|
|
|
}
|
|
|
|
|
}
|