mirror of
https://github.com/YGGverse/Yoda.git
synced 2025-01-30 13:04:13 +00:00
implement profile variants builder
This commit is contained in:
parent
01bef05346
commit
324c1044e3
@ -177,7 +177,7 @@ impl Browser {
|
|||||||
|
|
||||||
// Show welcome dialog on profile not selected yet (e.g. first launch)
|
// Show welcome dialog on profile not selected yet (e.g. first launch)
|
||||||
if self.profile.database.selected().is_none() {
|
if self.profile.database.selected().is_none() {
|
||||||
// @TODO Welcome::new(self.profile.clone()).present(Some(self.widget.gobject()));
|
// @TODO Welcome::new(self.profile.clone(), self.widget.gobject().clone()).present();
|
||||||
}
|
}
|
||||||
|
|
||||||
self
|
self
|
||||||
|
@ -2,7 +2,7 @@ mod widget;
|
|||||||
use widget::Widget;
|
use widget::Widget;
|
||||||
|
|
||||||
use crate::profile::Profile;
|
use crate::profile::Profile;
|
||||||
use gtk::prelude::IsA;
|
use adw::ApplicationWindow;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
pub struct Welcome {
|
pub struct Welcome {
|
||||||
@ -14,17 +14,39 @@ impl Welcome {
|
|||||||
// Construct
|
// Construct
|
||||||
|
|
||||||
/// Create new `Self` for given Profile
|
/// Create new `Self` for given Profile
|
||||||
pub fn new(profile: Rc<Profile>) -> Self {
|
pub fn new(profile: Rc<Profile>, parent: ApplicationWindow) -> Self {
|
||||||
Self {
|
// Init widget
|
||||||
profile,
|
let widget = Rc::new(Widget::new(parent));
|
||||||
widget: Rc::new(Widget::new()),
|
|
||||||
|
// Init events
|
||||||
|
widget.connect_response(|value| {
|
||||||
|
match value {
|
||||||
|
Some(id) => {
|
||||||
|
// Select profile by record ID @TODO
|
||||||
}
|
}
|
||||||
|
None => {
|
||||||
|
// Create new profile @TODO
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Return activated `Self`
|
||||||
|
Self { profile, widget }
|
||||||
}
|
}
|
||||||
|
|
||||||
// Actions
|
// Actions
|
||||||
|
|
||||||
/// Show dialog for parent [Window](https://docs.gtk.org/gtk4/class.Window.html)
|
/// Show dialog for parent [Window](https://docs.gtk.org/gtk4/class.Window.html)
|
||||||
pub fn present(&self, parent: Option<&impl IsA<gtk::Widget>>) {
|
pub fn present(&self) {
|
||||||
self.widget.present(parent);
|
// Collect Profile list
|
||||||
|
let mut responses = Vec::new();
|
||||||
|
for record in self.profile.database.records() {
|
||||||
|
responses.push((
|
||||||
|
record.id.to_string(),
|
||||||
|
record.time.format_iso8601().unwrap().to_string(),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
// Show dialog
|
||||||
|
self.widget.present(responses);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
use adw::{
|
use adw::{
|
||||||
prelude::{AdwDialogExt, AlertDialogExt, AlertDialogExtManual},
|
prelude::{AdwDialogExt, AlertDialogExt, AlertDialogExtManual},
|
||||||
AlertDialog, ResponseAppearance,
|
AlertDialog, ApplicationWindow, ResponseAppearance,
|
||||||
};
|
};
|
||||||
use gtk::prelude::IsA;
|
use gtk::prelude::GtkWindowExt;
|
||||||
|
use std::cell::RefCell;
|
||||||
|
|
||||||
const HEADING: &str = "Welcome!";
|
const HEADING: &str = "Welcome!";
|
||||||
const BODY: &str = "Select profile for browser data";
|
const BODY: &str = "Select profile for browser data";
|
||||||
@ -11,13 +12,15 @@ const RESPONSE_CREATE: (&str, &str) = ("create", "Create new profile");
|
|||||||
|
|
||||||
pub struct Widget {
|
pub struct Widget {
|
||||||
gobject: AlertDialog,
|
gobject: AlertDialog,
|
||||||
|
parent: ApplicationWindow,
|
||||||
|
responses: RefCell<Vec<String>>, // wanted to cleanup previous preset by key
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Widget {
|
impl Widget {
|
||||||
// Constructors
|
// Constructors
|
||||||
|
|
||||||
/// Create new `Self`
|
/// Create new `Self` for [Window](https://gnome.pages.gitlab.gnome.org/libadwaita/doc/1.3/class.ApplicationWindow.html)
|
||||||
pub fn new() -> Self {
|
pub fn new(parent: ApplicationWindow) -> Self {
|
||||||
// Init gobject
|
// Init gobject
|
||||||
let gobject = AlertDialog::builder()
|
let gobject = AlertDialog::builder()
|
||||||
.heading(HEADING)
|
.heading(HEADING)
|
||||||
@ -26,28 +29,56 @@ impl Widget {
|
|||||||
.default_response(RESPONSE_CREATE.0)
|
.default_response(RESPONSE_CREATE.0)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
// Init response variants
|
// Set response variants
|
||||||
gobject.add_responses(&[RESPONSE_QUIT, RESPONSE_CREATE]);
|
gobject.add_responses(&[RESPONSE_QUIT, RESPONSE_CREATE]);
|
||||||
|
|
||||||
// Decorate
|
// Decorate default response preset
|
||||||
gobject.set_response_appearance(RESPONSE_CREATE.0, ResponseAppearance::Suggested);
|
gobject.set_response_appearance(RESPONSE_CREATE.0, ResponseAppearance::Suggested);
|
||||||
gobject.set_response_appearance(RESPONSE_QUIT.0, ResponseAppearance::Destructive);
|
gobject.set_response_appearance(RESPONSE_QUIT.0, ResponseAppearance::Destructive);
|
||||||
|
|
||||||
// Return new `Self`
|
// Return new `Self`
|
||||||
Self { gobject }
|
Self {
|
||||||
|
gobject,
|
||||||
|
parent,
|
||||||
|
responses: RefCell::new(Vec::new()),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Actions
|
// Actions
|
||||||
|
|
||||||
/// Show dialog for parent [Window](https://docs.gtk.org/gtk4/class.Window.html)
|
/// Wrapper for default [response](https://gnome.pages.gitlab.gnome.org/libadwaita/doc/main/signal.AlertDialog.response.html) signal
|
||||||
pub fn present(&self, parent: Option<&impl IsA<gtk::Widget>>) {
|
/// * return profile ID, new record request on `None` or immediately close `self.parent` given on construction
|
||||||
self.gobject.present(parent);
|
pub fn connect_response(&self, callback: impl Fn(Option<i64>) + 'static) {
|
||||||
|
self.gobject.connect_response(None, {
|
||||||
|
let parent = self.parent.clone();
|
||||||
|
move |_, response| match response {
|
||||||
|
id if id == RESPONSE_CREATE.0 => callback(None),
|
||||||
|
id if id == RESPONSE_QUIT.0 => parent.close(),
|
||||||
|
_ => callback(Some(response.parse::<i64>().unwrap())),
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/* @TODO not in use
|
/// Show dialog with new profile responses preset
|
||||||
/// Get reference to GObject
|
pub fn present(&self, profiles: Vec<(String, String)>) {
|
||||||
///
|
// Borrow current index to update
|
||||||
pub fn gobject(&self) -> &AlertDialog {
|
let mut index = self.responses.borrow_mut();
|
||||||
&self.gobject
|
|
||||||
} */
|
// Remove previous responses from widget
|
||||||
|
for response in index.iter() {
|
||||||
|
self.gobject.remove_response(response)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reset index
|
||||||
|
index.clear();
|
||||||
|
|
||||||
|
// Build new preset
|
||||||
|
for (id, label) in profiles {
|
||||||
|
self.gobject.add_response(&id, &label);
|
||||||
|
index.push(id)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Show dialog
|
||||||
|
self.gobject.present(Some(&self.parent))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user