2020-12-26 02:05:34 -08:00
|
|
|
// window.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::subclass::prelude::*;
|
2020-12-26 02:05:34 -08:00
|
|
|
use glib::clone;
|
|
|
|
|
use gtk::prelude::*;
|
|
|
|
|
use gtk::subclass::prelude::*;
|
|
|
|
|
use gtk::CompositeTemplate;
|
|
|
|
|
|
2021-12-29 17:43:58 -08:00
|
|
|
use ashpd::desktop::open_uri;
|
|
|
|
|
use ashpd::WindowIdentifier;
|
|
|
|
|
|
2020-12-26 02:05:34 -08:00
|
|
|
use crate::config;
|
2021-12-27 15:03:31 -08:00
|
|
|
use crate::util;
|
2021-12-21 10:55:02 -08:00
|
|
|
use crate::widgets::LpImageView;
|
2020-12-26 02:05:34 -08:00
|
|
|
|
|
|
|
|
mod imp {
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
|
|
// To use composite templates, you need
|
|
|
|
|
// to use derive macro. Derive macros generate
|
|
|
|
|
// code to e.g. implement a trait on something.
|
|
|
|
|
// In this case, code is generated for Debug output
|
|
|
|
|
// and to handle binding the template children.
|
|
|
|
|
//
|
|
|
|
|
// For this derive macro, you need to have
|
|
|
|
|
// `use gtk::CompositeTemplate` in your code.
|
2021-06-27 15:28:35 -07:00
|
|
|
//
|
|
|
|
|
// Because all of our member fields implement the
|
|
|
|
|
// `Default` trait, we can use `#[derive(Default)]`.
|
|
|
|
|
// If some member fields did not implement default,
|
|
|
|
|
// we'd need to have a `new()` function in the
|
|
|
|
|
// `impl ObjectSubclass for $TYPE` section.
|
|
|
|
|
#[derive(Default, Debug, CompositeTemplate)]
|
2021-09-28 17:44:26 -07:00
|
|
|
#[template(resource = "/org/gnome/Loupe/gtk/window.ui")]
|
2021-12-21 10:55:02 -08:00
|
|
|
pub struct LpWindow {
|
2020-12-26 02:05:34 -08:00
|
|
|
// Template children are used with the
|
|
|
|
|
// TemplateChild<T> wrapper, where T is the
|
|
|
|
|
// object type of the template child.
|
|
|
|
|
#[template_child]
|
2021-12-21 12:15:46 -08:00
|
|
|
pub flap: TemplateChild<adw::Flap>,
|
2021-09-24 01:14:56 -07:00
|
|
|
#[template_child]
|
2020-12-26 02:05:34 -08:00
|
|
|
pub headerbar: TemplateChild<gtk::HeaderBar>,
|
|
|
|
|
#[template_child]
|
2021-09-24 01:14:56 -07:00
|
|
|
pub menu_button: TemplateChild<gtk::MenuButton>,
|
|
|
|
|
#[template_child]
|
|
|
|
|
pub fullscreen_button: TemplateChild<gtk::Button>,
|
|
|
|
|
#[template_child]
|
2021-12-28 16:38:36 -08:00
|
|
|
pub toast_overlay: TemplateChild<adw::ToastOverlay>,
|
|
|
|
|
#[template_child]
|
2020-12-26 02:05:34 -08:00
|
|
|
pub stack: TemplateChild<gtk::Stack>,
|
|
|
|
|
#[template_child]
|
2021-12-21 12:15:46 -08:00
|
|
|
pub status_page: TemplateChild<adw::StatusPage>,
|
2020-12-26 02:05:34 -08:00
|
|
|
#[template_child]
|
2021-12-21 10:55:02 -08:00
|
|
|
pub image_view: TemplateChild<LpImageView>,
|
2021-12-27 15:03:31 -08:00
|
|
|
#[template_child]
|
|
|
|
|
pub drop_target: TemplateChild<gtk::DropTarget>,
|
2020-12-26 02:05:34 -08:00
|
|
|
}
|
|
|
|
|
|
2021-06-27 15:28:35 -07:00
|
|
|
#[glib::object_subclass]
|
2021-12-21 10:55:02 -08:00
|
|
|
impl ObjectSubclass for LpWindow {
|
|
|
|
|
const NAME: &'static str = "LpWindow";
|
|
|
|
|
type Type = super::LpWindow;
|
2021-12-21 12:15:46 -08:00
|
|
|
type ParentType = adw::ApplicationWindow;
|
2020-12-26 02:05:34 -08:00
|
|
|
|
|
|
|
|
fn class_init(klass: &mut Self::Class) {
|
2021-01-30 15:52:32 -08:00
|
|
|
// bind_template() is a function generated by the
|
2020-12-26 02:05:34 -08:00
|
|
|
// CompositeTemplate macro to bind all children at once.
|
2021-01-30 15:52:32 -08:00
|
|
|
Self::bind_template(klass);
|
2021-09-22 21:33:47 -07:00
|
|
|
|
|
|
|
|
// Set up actions
|
|
|
|
|
klass.install_action("win.toggle-fullscreen", None, move |win, _, _| {
|
2021-09-24 01:14:56 -07:00
|
|
|
win.toggle_fullscreen(!win.is_fullscreened());
|
2021-09-22 21:33:47 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
klass.install_action("win.open", None, move |win, _, _| {
|
|
|
|
|
win.pick_file();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
klass.install_action("win.open-with", None, move |win, _, _| {
|
|
|
|
|
win.open_with();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
klass.install_action("win.set-wallpaper", None, move |win, _, _| {
|
|
|
|
|
win.set_wallpaper();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
klass.install_action("win.print", None, move |win, _, _| {
|
|
|
|
|
win.print();
|
|
|
|
|
});
|
2021-12-28 16:38:36 -08:00
|
|
|
|
2022-01-08 14:02:16 +01:00
|
|
|
klass.install_action("win.copy", None, move |win, _, _| {
|
|
|
|
|
win.copy();
|
|
|
|
|
});
|
|
|
|
|
|
2021-12-28 16:38:36 -08:00
|
|
|
klass.install_action("win.show-toast", Some("(si)"), move |win, _, var| {
|
2022-06-29 20:09:45 -04:00
|
|
|
if let Some((ref toast, i)) = var.and_then(|v| v.get::<(String, i32)>()) {
|
2021-12-28 16:38:36 -08:00
|
|
|
win.show_toast(toast, adw::ToastPriority::__Unknown(i));
|
|
|
|
|
}
|
|
|
|
|
});
|
2020-12-26 02:05:34 -08:00
|
|
|
}
|
2021-09-22 21:33:47 -07:00
|
|
|
|
2021-06-27 15:28:35 -07:00
|
|
|
fn instance_init(obj: &glib::subclass::InitializingObject<Self>) {
|
2021-01-13 23:05:45 -08:00
|
|
|
obj.init_template();
|
|
|
|
|
}
|
2020-12-26 02:05:34 -08:00
|
|
|
}
|
|
|
|
|
|
2021-12-21 10:55:02 -08:00
|
|
|
impl ObjectImpl for LpWindow {
|
2020-12-26 02:05:34 -08:00
|
|
|
fn constructed(&self, obj: &Self::Type) {
|
|
|
|
|
self.parent_constructed(obj);
|
|
|
|
|
|
|
|
|
|
if config::PROFILE == ".Devel" {
|
|
|
|
|
obj.add_css_class("devel");
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-22 21:33:47 -07:00
|
|
|
obj.set_actions_enabled(false);
|
2020-12-26 02:05:34 -08:00
|
|
|
|
|
|
|
|
self.status_page
|
|
|
|
|
.set_icon_name(Some(&format!("{}-symbolic", config::APP_ID)));
|
|
|
|
|
|
|
|
|
|
// Set help overlay
|
2021-09-28 17:44:26 -07:00
|
|
|
let builder = gtk::Builder::from_resource("/org/gnome/Loupe/gtk/help_overlay.ui");
|
2021-06-27 15:28:35 -07:00
|
|
|
let help_overlay = builder.object("help_overlay").unwrap();
|
2020-12-26 02:05:34 -08:00
|
|
|
obj.set_help_overlay(Some(&help_overlay));
|
|
|
|
|
|
|
|
|
|
// For callbacks, you will want to reference the GTK docs on
|
|
|
|
|
// the relevant signal to see which parameters you need.
|
2021-12-22 19:38:56 -08:00
|
|
|
// In this case, we need one to react to the signal,
|
|
|
|
|
// so we name it `iv` then use `_` for the other spots.
|
2021-09-05 05:51:34 -07:00
|
|
|
self.image_view.connect_notify_local(
|
|
|
|
|
Some("filename"),
|
|
|
|
|
clone!(@weak obj => move |iv, _| {
|
|
|
|
|
if let Some(filename) = iv.filename() {
|
|
|
|
|
obj.set_title(Some(&filename));
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
);
|
2021-12-27 15:03:31 -08:00
|
|
|
|
|
|
|
|
self.drop_target.set_types(&[gdk::FileList::static_type()]);
|
|
|
|
|
self.drop_target.connect_drop(
|
|
|
|
|
clone!(@weak obj => @default-return false, move |_, value, _, _| {
|
|
|
|
|
// Here we use a GValue, which is a dynamic object that can hold different types,
|
|
|
|
|
// e.g. strings, numbers, or in this case objects. In order to get the GdkFileList
|
|
|
|
|
// from the GValue, we need to use the `get()` method.
|
|
|
|
|
//
|
|
|
|
|
// We've added type annotations here, and written it as `let list: gdk::FileList = ...`,
|
|
|
|
|
// but you might also see places where type arguments are used.
|
|
|
|
|
// This line could have been written as `let list = value.get::<gdk::FileList>().unwrap()`.
|
|
|
|
|
let list: gdk::FileList = value.get().unwrap();
|
|
|
|
|
|
|
|
|
|
// TODO: Handle this like EOG and make a "directory" out of the given files
|
|
|
|
|
let file = list.files().get(0).unwrap().clone();
|
|
|
|
|
let info = util::query_attributes(
|
|
|
|
|
&file,
|
2021-12-28 16:38:36 -08:00
|
|
|
vec![
|
|
|
|
|
&gio::FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
|
|
|
|
|
&gio::FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME,
|
|
|
|
|
],
|
|
|
|
|
)
|
|
|
|
|
.expect("Could not query file info");
|
2021-12-27 15:03:31 -08:00
|
|
|
|
|
|
|
|
if info
|
|
|
|
|
.content_type()
|
|
|
|
|
.map(|t| t.to_string())
|
|
|
|
|
.filter(|t| t.starts_with("image/"))
|
|
|
|
|
.is_some() {
|
|
|
|
|
obj.set_image_from_file(&file, false);
|
|
|
|
|
} else {
|
2021-12-28 16:38:36 -08:00
|
|
|
obj.show_toast(
|
2022-02-04 15:25:12 -08:00
|
|
|
&format!("\"{}\" is not a valid image.", info.display_name()),
|
2021-12-28 16:38:36 -08:00
|
|
|
adw::ToastPriority::High,
|
|
|
|
|
);
|
2021-12-27 15:03:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
true
|
|
|
|
|
}),
|
|
|
|
|
);
|
2020-12-26 02:05:34 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-21 10:55:02 -08:00
|
|
|
impl WidgetImpl for LpWindow {}
|
|
|
|
|
impl WindowImpl for LpWindow {}
|
|
|
|
|
impl ApplicationWindowImpl for LpWindow {}
|
|
|
|
|
impl AdwApplicationWindowImpl for LpWindow {}
|
2020-12-26 02:05:34 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
glib::wrapper! {
|
2021-12-21 10:55:02 -08:00
|
|
|
pub struct LpWindow(ObjectSubclass<imp::LpWindow>)
|
2021-12-21 12:15:46 -08:00
|
|
|
@extends gtk::Widget, gtk::Window, gtk::ApplicationWindow, adw::ApplicationWindow,
|
2021-12-29 17:43:58 -08:00
|
|
|
@implements gio::ActionMap, gio::ActionGroup, gtk::Native;
|
2020-12-26 02:05:34 -08:00
|
|
|
}
|
|
|
|
|
|
2021-12-21 10:55:02 -08:00
|
|
|
impl LpWindow {
|
2020-12-26 02:05:34 -08:00
|
|
|
pub fn new<A: IsA<gtk::Application>>(app: &A) -> Self {
|
2021-12-21 10:55:02 -08:00
|
|
|
glib::Object::new(&[("application", app)]).expect("Failed to create LpWindow")
|
2020-12-26 02:05:34 -08:00
|
|
|
}
|
|
|
|
|
|
2021-09-24 01:14:56 -07:00
|
|
|
fn toggle_fullscreen(&self, fullscreen: bool) {
|
2021-12-29 14:02:48 -08:00
|
|
|
let imp = self.imp();
|
2021-09-24 01:14:56 -07:00
|
|
|
|
|
|
|
|
if fullscreen {
|
2021-12-21 12:15:46 -08:00
|
|
|
imp.flap.set_fold_policy(adw::FlapFoldPolicy::Always);
|
2021-09-24 01:14:56 -07:00
|
|
|
imp.headerbar.add_css_class("osd");
|
|
|
|
|
imp.menu_button.unparent();
|
|
|
|
|
imp.fullscreen_button.unparent();
|
|
|
|
|
imp.headerbar.pack_end(&*imp.fullscreen_button);
|
|
|
|
|
imp.headerbar.pack_end(&*imp.menu_button);
|
|
|
|
|
imp.fullscreen_button.set_icon_name("view-restore-symbolic");
|
|
|
|
|
} else {
|
2021-12-21 12:15:46 -08:00
|
|
|
imp.flap.set_fold_policy(adw::FlapFoldPolicy::Never);
|
2021-09-24 01:14:56 -07:00
|
|
|
imp.headerbar.remove_css_class("osd");
|
|
|
|
|
imp.menu_button.unparent();
|
|
|
|
|
imp.fullscreen_button.unparent();
|
|
|
|
|
imp.headerbar.pack_end(&*imp.menu_button);
|
|
|
|
|
imp.headerbar.pack_end(&*imp.fullscreen_button);
|
|
|
|
|
imp.fullscreen_button
|
|
|
|
|
.set_icon_name("view-fullscreen-symbolic");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.set_fullscreened(fullscreen);
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-26 02:05:34 -08:00
|
|
|
fn pick_file(&self) {
|
|
|
|
|
let chooser = gtk::FileChooserNative::new(
|
2022-06-29 20:10:37 -04:00
|
|
|
Some("Open Image"),
|
2020-12-26 02:05:34 -08:00
|
|
|
Some(self),
|
|
|
|
|
gtk::FileChooserAction::Open,
|
|
|
|
|
None,
|
|
|
|
|
None,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
chooser.set_modal(true);
|
|
|
|
|
chooser.set_transient_for(Some(self));
|
|
|
|
|
|
|
|
|
|
let filter = gtk::FileFilter::new();
|
2021-12-22 18:17:38 -08:00
|
|
|
filter.set_property("name", &String::from("Supported image files"));
|
2020-12-26 02:05:34 -08:00
|
|
|
filter.add_mime_type("image/*");
|
|
|
|
|
chooser.add_filter(&filter);
|
|
|
|
|
|
|
|
|
|
chooser.connect_response(
|
|
|
|
|
clone!(@weak self as win, @strong chooser => move |_, resp| {
|
|
|
|
|
if resp == gtk::ResponseType::Accept {
|
2021-06-27 15:28:35 -07:00
|
|
|
if let Some(file) = chooser.file() {
|
2021-12-27 15:03:31 -08:00
|
|
|
win.set_image_from_file(&file, true);
|
2020-12-26 02:05:34 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
chooser.show();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn open_with(&self) {
|
2021-12-29 14:02:48 -08:00
|
|
|
let imp = self.imp();
|
2021-12-29 17:43:58 -08:00
|
|
|
let ctx = glib::MainContext::default();
|
|
|
|
|
|
|
|
|
|
if let Some(file) = imp
|
|
|
|
|
.image_view
|
2022-02-05 13:26:33 -08:00
|
|
|
.current_page()
|
|
|
|
|
.and_then(|p| p.file())
|
|
|
|
|
.and_then(|f| f.peek_path())
|
|
|
|
|
.and_then(|p| std::fs::File::open(p).ok())
|
2021-12-29 17:43:58 -08:00
|
|
|
{
|
|
|
|
|
ctx.spawn_local(clone!(@weak self as win => async move {
|
|
|
|
|
if let Err(e) =
|
|
|
|
|
open_uri::open_file(&WindowIdentifier::from_native(&win).await, &file, true, true).await
|
|
|
|
|
{
|
|
|
|
|
log::error!("Could not open image in external program: {}", e);
|
|
|
|
|
}
|
|
|
|
|
}));
|
2020-12-26 02:05:34 -08:00
|
|
|
} else {
|
2022-02-05 13:26:33 -08:00
|
|
|
log::error!("Could not load a path for the current image.")
|
2020-12-26 02:05:34 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn set_wallpaper(&self) {
|
2021-12-29 14:02:48 -08:00
|
|
|
let imp = self.imp();
|
2020-12-26 02:05:34 -08:00
|
|
|
|
2021-01-13 23:16:02 -08:00
|
|
|
if let Err(e) = imp.image_view.set_wallpaper() {
|
2020-12-26 02:05:34 -08:00
|
|
|
log::error!("Failed to set wallpaper: {}", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn print(&self) {
|
2021-12-29 14:02:48 -08:00
|
|
|
let imp = self.imp();
|
2020-12-26 02:05:34 -08:00
|
|
|
|
2021-01-13 23:16:02 -08:00
|
|
|
if let Err(e) = imp.image_view.print() {
|
2020-12-26 02:05:34 -08:00
|
|
|
log::error!("Failed to print file: {}", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-08 14:02:16 +01:00
|
|
|
fn copy(&self) {
|
|
|
|
|
let imp = self.imp();
|
|
|
|
|
|
|
|
|
|
if let Err(e) = imp.image_view.copy() {
|
|
|
|
|
log::error!("Failed to copy to clipboard: {}", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-28 16:38:36 -08:00
|
|
|
fn show_toast(&self, text: &impl AsRef<str>, priority: adw::ToastPriority) {
|
2021-12-29 14:02:48 -08:00
|
|
|
let imp = self.imp();
|
2021-12-28 16:38:36 -08:00
|
|
|
|
|
|
|
|
let toast = adw::Toast::new(text.as_ref());
|
|
|
|
|
toast.set_priority(priority);
|
|
|
|
|
|
|
|
|
|
imp.toast_overlay.add_toast(&toast);
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-19 23:11:38 -08:00
|
|
|
pub fn set_image_from_file(&self, file: &gio::File, _resize: bool) {
|
2021-12-29 14:02:48 -08:00
|
|
|
let imp = self.imp();
|
2020-12-26 02:05:34 -08:00
|
|
|
|
2021-06-27 15:28:35 -07:00
|
|
|
log::debug!("Loading file: {}", file.uri().to_string());
|
2021-09-28 22:08:57 -07:00
|
|
|
match imp.image_view.set_image_from_file(file) {
|
2022-01-19 23:11:38 -08:00
|
|
|
Ok((/*width, height*/)) => {
|
|
|
|
|
// if resize {
|
|
|
|
|
// self.resize_from_dimensions(width, height);
|
|
|
|
|
// }
|
2021-12-27 15:03:31 -08:00
|
|
|
|
2021-09-28 22:08:57 -07:00
|
|
|
imp.stack.set_visible_child(&*imp.image_view);
|
|
|
|
|
imp.image_view.grab_focus();
|
|
|
|
|
self.set_actions_enabled(true)
|
|
|
|
|
}
|
|
|
|
|
Err(e) => log::error!("Could not load file: {}", e.to_string()),
|
|
|
|
|
}
|
2020-12-26 02:05:34 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn set_actions_enabled(&self, enabled: bool) {
|
2021-09-22 21:33:47 -07:00
|
|
|
self.action_set_enabled("win.open-with", enabled);
|
|
|
|
|
self.action_set_enabled("win.set-wallpaper", enabled);
|
|
|
|
|
self.action_set_enabled("win.toggle-fullscreen", enabled);
|
|
|
|
|
self.action_set_enabled("win.print", enabled);
|
2022-01-08 14:02:16 +01:00
|
|
|
self.action_set_enabled("win.copy", enabled);
|
2020-12-26 02:05:34 -08:00
|
|
|
}
|
2021-03-02 16:39:03 -08:00
|
|
|
|
|
|
|
|
// Adapted from https://gitlab.gnome.org/GNOME/eog/-/blob/master/src/eog-window.c:eog_window_obtain_desired_size
|
|
|
|
|
pub fn resize_from_dimensions(&self, img_width: i32, img_height: i32) {
|
2021-12-29 14:02:48 -08:00
|
|
|
let imp = self.imp();
|
2021-03-02 16:39:03 -08:00
|
|
|
let mut final_width = img_width;
|
|
|
|
|
let mut final_height = img_height;
|
|
|
|
|
|
2021-06-27 15:28:35 -07:00
|
|
|
let header_height = imp.headerbar.height();
|
2021-03-02 16:39:03 -08:00
|
|
|
|
|
|
|
|
// Ensure the window surface exists
|
2021-06-27 15:28:35 -07:00
|
|
|
if !self.is_realized() {
|
2021-03-02 16:39:03 -08:00
|
|
|
self.realize();
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-27 15:28:35 -07:00
|
|
|
let display = gdk::Display::default().unwrap();
|
2022-01-08 22:18:12 -08:00
|
|
|
let monitor = display.monitor_at_surface(&self.native().unwrap().surface());
|
2021-06-27 15:28:35 -07:00
|
|
|
let monitor_geometry = monitor.geometry();
|
2021-03-02 16:39:03 -08:00
|
|
|
|
2021-12-22 18:17:38 -08:00
|
|
|
let monitor_width = monitor_geometry.width();
|
|
|
|
|
let monitor_height = monitor_geometry.height();
|
2021-03-02 16:39:03 -08:00
|
|
|
|
|
|
|
|
if img_width > monitor_width || img_height + header_height > monitor_height {
|
|
|
|
|
let width_factor = (monitor_width as f32 * 0.85) / img_width as f32;
|
|
|
|
|
let height_factor =
|
|
|
|
|
(monitor_height as f32 * 0.85 - header_height as f32) / img_height as f32;
|
|
|
|
|
let factor = width_factor.min(height_factor);
|
|
|
|
|
|
|
|
|
|
final_width = (final_width as f32 * factor).round() as i32;
|
|
|
|
|
final_height = (final_height as f32 * factor).round() as i32;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.set_default_size(final_width, final_height);
|
|
|
|
|
log::debug!("Window resized to {} x {}", final_width, final_height);
|
|
|
|
|
}
|
2020-12-26 02:05:34 -08:00
|
|
|
}
|