Commit 2c27bf49 authored by Bilal Elmoussaoui's avatar Bilal Elmoussaoui

Save/restore latest opened file

parent e9d958b5
Pipeline #103743 passed with stages
in 15 minutes and 28 seconds
......@@ -24,7 +24,10 @@
<key name="is-maximized" type="b">
<default>false</default>
<summary>Default window maximized behaviour</summary>
<description></description>
</key>
<key name="last-open-file" type="s">
<default>''</default>
<summary>The latest opened file</summary>
</key>
</schema>
</schemalist>
......@@ -13,6 +13,7 @@ use std::{cell::RefCell, rc::Rc};
use glib::{Receiver, Sender};
pub enum Action {
OpenFile,
LoadFile(String),
ViewEmpty,
ViewBanners,
AddError(String, String), // (id, error)
......@@ -124,17 +125,26 @@ impl Application {
open_dialog.add_filter(&xml_filter);
if gtk::ResponseType::from(open_dialog.run()) == gtk::ResponseType::Accept {
let file = open_dialog.get_file().unwrap();
self.sender.send(Action::ViewEmpty).unwrap();
self.banners.load_file(file);
self.sender.send(Action::ViewBanners).unwrap();
let file = open_dialog.get_filename().unwrap();
self.sender.send(Action::LoadFile(file.to_str().unwrap().to_string())).unwrap();
};
open_dialog.destroy();
}
fn load_file(&self, filepath: String) {
let file = gio::File::new_for_path(&filepath);
self.sender.send(Action::ViewEmpty).unwrap();
self.banners.load_file(file);
self.sender.send(Action::ViewBanners).unwrap();
}
fn do_action(&self, action: Action) -> glib::Continue {
match action {
Action::OpenFile => self.open_file(),
Action::LoadFile(file) => {
self.load_file(file.clone());
self.window.set_loaded_file(file);
}
Action::ViewEmpty => {
self.window.set_view(View::Empty);
}
......
......@@ -7,7 +7,7 @@ use gio::prelude::*;
use glib::Sender;
use gtk::prelude::*;
use libhandy::{Column, ColumnExt};
use std::cell::Ref;
use std::cell::{Ref, RefCell};
use std::collections::HashMap;
pub enum View {
......@@ -20,6 +20,7 @@ pub struct Window {
sender: Sender<Action>,
builder: gtk::Builder,
banners_container: gtk::Box,
settings: RefCell<gio::Settings>,
}
impl Window {
......@@ -36,9 +37,10 @@ impl Window {
sender,
builder,
banners_container,
settings: RefCell::new(settings),
};
window.init(settings);
window.init();
window.setup_actions();
window
}
......@@ -57,19 +59,28 @@ impl Window {
pub fn set_view(&self, view: View) {
let main_stack: gtk::Stack = self.builder.get_object("main_stack").unwrap();
let headerbar: gtk::HeaderBar = self.builder.get_object("headerbar").unwrap();
match view {
View::Empty => {
for child in self.banners_container.get_children() {
self.banners_container.remove(&child);
}
headerbar.set_has_subtitle(false);
main_stack.set_visible_child_name("empty_state");
}
View::Banners => {
headerbar.set_has_subtitle(true);
main_stack.set_visible_child_name("banners");
}
}
}
pub fn set_loaded_file(&self, file: String) {
let headerbar: gtk::HeaderBar = self.builder.get_object("headerbar").unwrap();
headerbar.set_subtitle(Some(&file));
self.settings.borrow_mut().set_string("last-open-file", &file);
}
pub fn set_errors(&self, errors: Ref<HashMap<String, String>>) {
let errors_togglebtn: gtk::ToggleButton = self.builder.get_object("errors_togglebtn").unwrap();
let errors_count_label: gtk::Label = self.builder.get_object("errors_count_label").unwrap();
......@@ -108,14 +119,15 @@ impl Window {
errors_togglebtn.set_sensitive(errors_count != 0);
}
fn init(&self, settings: gio::Settings) {
fn init(&self) {
// setup app menu
let appmenu_btn: gtk::MenuButton = self.builder.get_object("appmenu_button").unwrap();
let menu_builder = gtk::Builder::new_from_resource("/org/gnome/design/BannerViewer/menu.ui");
let popover_menu: gio::MenuModel = menu_builder.get_object("popover_menu").unwrap();
appmenu_btn.set_menu_model(Some(&popover_menu));
// load latest window state
window_state::load(&self.widget, &settings);
let settings = self.settings.borrow().clone();
window_state::load(&self.widget, &settings, self.sender.clone());
// save window state on delete event
self.widget.connect_delete_event(move |window, _| {
window_state::save(&window, &settings);
......
use crate::application::Action;
use gio::prelude::SettingsExt;
use glib::Sender;
use gtk::prelude::GtkWindowExt;
use std::path::Path;
pub fn load(window: &gtk::ApplicationWindow, settings: &gio::Settings) {
pub fn load(window: &gtk::ApplicationWindow, settings: &gio::Settings, sender: Sender<Action>) {
let width = settings.get_int("window-width");
let height = settings.get_int("window-height");
......@@ -13,6 +16,12 @@ pub fn load(window: &gtk::ApplicationWindow, settings: &gio::Settings) {
let y = settings.get_int("window-y");
let is_maximized = settings.get_boolean("is-maximized");
let filepath = settings.get_string("last-open-file").unwrap();
let path = Path::new(&filepath);
if path.exists() {
sender.send(Action::LoadFile(path.to_str().unwrap().to_string())).unwrap();
}
if x > -1 && y > -1 {
window.move_(x, y);
} else if is_maximized {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment