From af1d7c4a015ac179dd4f90b16584f127891b119d Mon Sep 17 00:00:00 2001 From: Elias Projahn Date: Mon, 28 Sep 2020 20:51:56 +0200 Subject: [PATCH] Use libhandy for the main window This also reverts a part of the changes from the last commit. They will be replaced by a new solution. --- Cargo.toml | 1 + res/ui/window.ui | 182 ++++++++++++++++++++++++++++------------------- src/main.rs | 3 + src/window.rs | 84 +++++++++------------- 4 files changed, 147 insertions(+), 123 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 2111ca1..95aeaca 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,5 +10,6 @@ gio = "0.9.1" glib = "0.10.2" gtk = { version = "0.9.2", features = ["v3_24"] } gtk-macros = "0.2.0" +libhandy = "0.7.0" pango = "0.9.1" rand = "0.7.3" diff --git a/res/ui/window.ui b/res/ui/window.ui index b0e3242..80c63bb 100644 --- a/res/ui/window.ui +++ b/res/ui/window.ui @@ -1,20 +1,70 @@ - - + + + False 800 566 - + True - True + sidebar_box + True - + True False vertical + 220 + + + True + False + True + + + True + True + True + add_menu + + + True + False + list-add-symbolic + + + + + + + True + True + False + True + add_menu + + + True + False + open-menu-symbolic + + + + + end + 0 + + + + + False + True + 0 + + True @@ -26,12 +76,12 @@ edit-find-symbolic False False - Search composers … + Search persons and ensembles … False True - 0 + 1 @@ -62,63 +112,67 @@ True True - 1 + 2 + + + + True + False + vertical + - False - True + False - + True - True + vertical - + True False - vertical + crossfade - + True - True - 6 - 6 - 6 - 6 - 6 - 6 - edit-find-symbolic - False - False - Search works … + False + true + Musicus + True - False - True - 0 + empty_header + empty_header - + True - True + False + true + Musicus + True - + + + + crossfade True - False - none - + + win.back True - False - - + True + True + + True False - No works found. + go-previous-symbolic @@ -127,46 +181,16 @@ - True - True - 1 + header + header - - False - True - - - - - - True - True - - - - - - - True - False - Musicus Editor - True - - - True - True - False - True - add_menu - - + True False - list-add-symbolic @@ -174,6 +198,18 @@ + + + + + + + + + + + +
@@ -198,4 +234,4 @@
- + \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 7d8203e..9684259 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,6 +18,9 @@ mod window; use window::Window; fn main() { + gtk::init().expect("Failed to initialize GTK!"); + libhandy::init(); + let bytes = glib::Bytes::from(include_bytes!("../res/resources.gresource").as_ref()); let resource = gio::Resource::from_data(&bytes).expect("Failed to load resources!"); gio::resources_register(&resource); diff --git a/src/window.rs b/src/window.rs index e7cdc27..72b2bfd 100644 --- a/src/window.rs +++ b/src/window.rs @@ -4,29 +4,37 @@ use gio::prelude::*; use glib::clone; use gtk::prelude::*; use gtk_macros::{action, get_widget}; +use libhandy::prelude::*; +use libhandy::HeaderBarExt; use std::cell::RefCell; use std::convert::TryInto; use std::rc::Rc; pub struct Window { - window: gtk::ApplicationWindow, + window: libhandy::ApplicationWindow, db: Rc, + leaflet: libhandy::Leaflet, persons: RefCell>, person_search_entry: gtk::SearchEntry, person_list: gtk::ListBox, - works: RefCell>, - work_search_entry: gtk::SearchEntry, - work_list: gtk::ListBox, + header_stack: gtk::Stack, + header: libhandy::HeaderBar, + content_box: gtk::Box, + content: gtk::ScrolledWindow, } impl Window { pub fn new(app: >k::Application) -> Rc { let builder = gtk::Builder::from_resource("/de/johrpan/musicus_editor/ui/window.ui"); - get_widget!(builder, gtk::ApplicationWindow, window); + + get_widget!(builder, libhandy::ApplicationWindow, window); + get_widget!(builder, libhandy::Leaflet, leaflet); get_widget!(builder, gtk::SearchEntry, person_search_entry); get_widget!(builder, gtk::ListBox, person_list); - get_widget!(builder, gtk::SearchEntry, work_search_entry); - get_widget!(builder, gtk::ListBox, work_list); + get_widget!(builder, gtk::Stack, header_stack); + get_widget!(builder, libhandy::HeaderBar, header); + get_widget!(builder, gtk::Box, content_box); + get_widget!(builder, gtk::ScrolledWindow, content); let db = Rc::new(Database::new("test.sqlite")); let persons = db.get_persons(); @@ -34,12 +42,14 @@ impl Window { let result = Rc::new(Window { window: window, db: db, + leaflet: leaflet, persons: RefCell::new(persons), person_list: person_list, person_search_entry: person_search_entry, - works: RefCell::new(Vec::new()), - work_search_entry: work_search_entry, - work_list: work_list, + header_stack: header_stack, + header: header, + content_box: content_box, + content: content, }); result @@ -47,10 +57,8 @@ impl Window { .connect_row_activated(clone!(@strong result => move |_, row| { let row = row.get_child().unwrap().downcast::().unwrap(); let index: usize = row.get_index().try_into().unwrap(); - - let works = result.db.get_work_descriptions(result.persons.borrow()[index].id); - result.works.replace(works); - result.show_works(); + let person = result.persons.borrow()[index].clone(); + result.show_person(person); })); result @@ -72,32 +80,6 @@ impl Window { result.person_list.invalidate_filter(); })); - // result - // .work_list - // .connect_row_activated(clone!(@strong result => move |_, row| { - // let row = row.get_child().unwrap().downcast::().unwrap(); - // let index: usize = row.get_index().try_into().unwrap(); - // })); - - result - .work_list - .set_filter_func(Some(Box::new(clone!(@strong result => move |row| { - let row = row.get_child().unwrap().downcast::().unwrap(); - let index: usize = row.get_index().try_into().unwrap(); - let search = result.work_search_entry.get_text().to_string().to_lowercase(); - - search.is_empty() || result.works.borrow()[index] - .title - .to_lowercase() - .contains(&search) - })))); - - result - .work_search_entry - .connect_search_changed(clone!(@strong result => move |_| { - result.work_list.invalidate_filter(); - })); - action!( result.window, "add-person", @@ -168,17 +150,19 @@ impl Window { } } - fn show_works(&self) { - for child in self.work_list.get_children() { - self.work_list.remove(&child); + fn show_person(&self, person: Person) { + self.header.set_title(Some(&person.name_fl())); + self.header_stack.set_visible_child_name("header"); + self.set_view(>k::Label::new(Some(&person.name_fl()))); + } + + fn set_view>(&self, widget: &T) { + match self.content.get_child() { + Some(child) => self.content.remove(&child), + None => (), } - for (index, work) in self.works.borrow().iter().enumerate() { - let label = gtk::Label::new(Some(&work.title)); - label.set_halign(gtk::Align::Start); - let row = SelectorRow::new(index.try_into().unwrap(), &label); - row.show_all(); - self.work_list.insert(&row, -1); - } + self.content.add(widget); + self.leaflet.set_visible_child(&self.content_box); } }