-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
yggverse
committed
Feb 6, 2025
1 parent
6267691
commit f6fb73c
Showing
11 changed files
with
289 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
src/app/browser/window/tab/item/page/input/titan/header.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
mod form; | ||
|
||
use gtk::{glib::GString, prelude::IsA, Widget}; | ||
|
||
#[derive(Default)] | ||
pub struct Header { | ||
pub mime: Option<GString>, | ||
pub token: Option<GString>, | ||
} | ||
|
||
impl Header { | ||
pub fn new() -> Self { | ||
Self { | ||
mime: None, | ||
token: None, | ||
} | ||
} | ||
|
||
/// Show header options dialog for the referrer `widget` | ||
/// * takes ownership of `Self`, return new updated copy in `callback` function | ||
pub fn dialog(self, widget: Option<&impl IsA<Widget>>, callback: impl Fn(Self) + 'static) { | ||
use adw::{ | ||
prelude::{AdwDialogExt, AlertDialogExt, AlertDialogExtManual}, | ||
AlertDialog, ResponseAppearance, | ||
}; | ||
use form::Form; | ||
use std::rc::Rc; | ||
|
||
// Response variants | ||
const RESPONSE_APPLY: (&str, &str) = ("apply", "Apply"); | ||
const RESPONSE_CANCEL: (&str, &str) = ("cancel", "Cancel"); | ||
|
||
// Init form components | ||
let form = Rc::new(Form::build( | ||
&self.mime.unwrap_or_default(), | ||
&self.token.unwrap_or_default(), | ||
)); | ||
|
||
// Init main widget | ||
let alert_dialog = AlertDialog::builder() | ||
.heading("Header") | ||
.body("Custom header options") | ||
.close_response(RESPONSE_CANCEL.0) | ||
.default_response(RESPONSE_APPLY.0) | ||
.extra_child(&form.g_box) | ||
.build(); | ||
|
||
alert_dialog.add_responses(&[RESPONSE_CANCEL, RESPONSE_APPLY]); | ||
|
||
// Decorate default response preset | ||
alert_dialog.set_response_appearance(RESPONSE_APPLY.0, ResponseAppearance::Suggested); | ||
/* contrast issue with Ubuntu orange accents | ||
alert_dialog.set_response_appearance(RESPONSE_CANCEL.0, ResponseAppearance::Destructive); */ | ||
|
||
// Init events | ||
|
||
alert_dialog.connect_response(None, { | ||
let form = form.clone(); | ||
move |this, response| { | ||
this.set_response_enabled(response, false); // prevent double-click | ||
if response == RESPONSE_APPLY.0 { | ||
callback(Self { | ||
mime: form.mime(), | ||
token: form.token(), | ||
}) | ||
} else { | ||
// @TODO restore | ||
} | ||
} | ||
}); | ||
|
||
// Show | ||
alert_dialog.present(widget); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
src/app/browser/window/tab/item/page/input/titan/header/form.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
mod mime; | ||
mod token; | ||
|
||
use mime::Mime; | ||
use token::Token; | ||
|
||
use gtk::{ | ||
glib::GString, | ||
prelude::{BoxExt, EditableExt}, | ||
Box, Entry, Orientation, | ||
}; | ||
|
||
pub struct Form { | ||
pub g_box: Box, | ||
mime: Entry, | ||
token: Entry, | ||
} | ||
|
||
impl Form { | ||
// Constructors | ||
|
||
pub fn build(mime_value: &str, token_value: &str) -> Self { | ||
// Init components | ||
let mime = Entry::mime(mime_value); | ||
let token = Entry::token(token_value); | ||
|
||
// Init `Self` | ||
let g_box = Box::builder().orientation(Orientation::Vertical).build(); | ||
|
||
g_box.append(&mime); | ||
g_box.append(&token); | ||
|
||
Self { g_box, mime, token } | ||
} | ||
|
||
// Getters | ||
|
||
pub fn mime(&self) -> Option<GString> { | ||
value(&self.mime) | ||
} | ||
|
||
pub fn token(&self) -> Option<GString> { | ||
value(&self.token) | ||
} | ||
} | ||
|
||
// Tools | ||
|
||
fn value(label: &Entry) -> Option<GString> { | ||
let text = label.text(); | ||
if !text.is_empty() { | ||
Some(text) | ||
} else { | ||
None | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/app/browser/window/tab/item/page/input/titan/header/form/mime.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
pub trait Mime { | ||
fn mime(text: &str) -> Self; | ||
fn validate(&self); | ||
} | ||
|
||
impl Mime for gtk::Entry { | ||
fn mime(text: &str) -> Self { | ||
use gtk::prelude::EditableExt; | ||
|
||
let mime = gtk::Entry::builder() | ||
.placeholder_text("Content type (MIME)") | ||
.margin_bottom(8) | ||
.text(text) | ||
.build(); | ||
|
||
mime.connect_changed(|this| { | ||
this.validate(); | ||
}); | ||
|
||
mime | ||
} | ||
|
||
fn validate(&self) { | ||
use gtk::prelude::{EditableExt, WidgetExt}; | ||
|
||
const CLASS: (&str, &str) = ("error", "success"); | ||
|
||
self.remove_css_class(CLASS.0); | ||
self.remove_css_class(CLASS.1); | ||
|
||
if !self.text().is_empty() { | ||
if gtk::glib::Regex::match_simple( | ||
r"^\w+/\w+$", | ||
self.text(), | ||
gtk::glib::RegexCompileFlags::DEFAULT, | ||
gtk::glib::RegexMatchFlags::DEFAULT, | ||
) { | ||
self.add_css_class(CLASS.1) | ||
} else { | ||
self.add_css_class(CLASS.0) | ||
} | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/app/browser/window/tab/item/page/input/titan/header/form/token.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
use gtk::Entry; | ||
|
||
pub trait Token { | ||
fn token(text: &str) -> Self; | ||
} | ||
|
||
impl Token for Entry { | ||
fn token(text: &str) -> Self { | ||
Entry::builder() | ||
.placeholder_text("Token") | ||
.text(text) | ||
.build() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.