Skip to content

Commit

Permalink
reorganize children widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
yggverse committed Jan 22, 2025
1 parent ba96581 commit 972fa6c
Show file tree
Hide file tree
Showing 21 changed files with 271 additions and 488 deletions.
2 changes: 1 addition & 1 deletion src/app/browser/window/tab/item/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl Page {
&navigation.widget.g_box,
&content.g_box,
&search.g_box,
&input.widget.clamp,
&input.clamp,
));

// Done
Expand Down
46 changes: 27 additions & 19 deletions src/app/browser/window/tab/item/page/input.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
mod response;
mod sensitive;
mod titan;
mod widget;

use super::TabAction;
use gtk::{glib::Uri, Label};
use adw::Clamp;
use gtk::{glib::Uri, prelude::WidgetExt, Box, Label};
use response::Response;
use sensitive::Sensitive;
use std::rc::Rc;
use titan::Titan;
use widget::Widget;

const MARGIN: i32 = 6;

pub struct Input {
pub widget: Rc<Widget>,
pub clamp: Clamp,
}

impl Default for Input {
Expand All @@ -24,16 +25,28 @@ impl Default for Input {
impl Input {
// Construct
pub fn new() -> Self {
// Init widget
let widget = Rc::new(Widget::new());
let clamp = Clamp::builder()
.margin_bottom(MARGIN)
.margin_top(MARGIN)
.maximum_size(800)
.visible(false)
.build();

// Result
Self { widget }
Self { clamp }
}

// Actions
pub fn unset(&self) {
self.widget.update(None);
self.update(None);
}

pub fn update(&self, child: Option<&Box>) {
if child.is_some() {
self.clamp.set_visible(true); // widget may be hidden, make it visible to child redraw
self.clamp.set_child(child);
} else {
self.clamp.set_visible(false)
}
}

// Setters
Expand All @@ -44,10 +57,8 @@ impl Input {
title: Option<&str>,
size_limit: Option<usize>,
) {
self.widget.update(Some(
&Response::build(action, base, title, size_limit)
.widget
.g_box,
self.update(Some(
&Response::build(action, base, title, size_limit).g_box,
));
}

Expand All @@ -58,15 +69,12 @@ impl Input {
title: Option<&str>,
max_length: Option<i32>,
) {
self.widget.update(Some(
&Sensitive::build(action, base, title, max_length)
.widget
.g_box,
self.update(Some(
&Sensitive::build(action, base, title, max_length).g_box,
));
}

pub fn set_new_titan(&self, on_send: impl Fn(&[u8], &Label) + 'static) {
self.widget
.update(Some(&Titan::build(on_send).widget.g_box));
self.update(Some(&Titan::build(on_send).g_box));
}
}
36 changes: 23 additions & 13 deletions src/app/browser/window/tab/item/page/input/response.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
mod control;
mod form;
mod title;
mod widget;

use control::Control;
use form::Form;
use title::Title;
use widget::Widget;

use super::TabAction;
use gtk::{
gio::SimpleAction,
glib::{uuid_string_random, Uri, UriHideFlags},
prelude::BoxExt,
Box, Orientation,
};
use std::rc::Rc;

const MARGIN: i32 = 6;
const SPACING: i32 = 8;

pub struct Response {
// Components
pub widget: Rc<Widget>,
pub g_box: Box,
}

impl Response {
Expand All @@ -39,12 +42,19 @@ impl Response {
let form = Rc::new(Form::build(action_update.clone()));
let title = Rc::new(Title::build(title));

// Init widget
let widget = Rc::new(Widget::build(
&title.widget.label,
&form.widget.text_view,
&control.widget.g_box,
));
// Init main widget
let g_box = Box::builder()
.margin_bottom(MARGIN)
.margin_end(MARGIN)
.margin_start(MARGIN)
.margin_top(MARGIN)
.spacing(SPACING)
.orientation(Orientation::Vertical)
.build();

g_box.append(&title.label);
g_box.append(&form.text_view);
g_box.append(&control.g_box);

// Init events
action_update.connect_activate({
Expand All @@ -53,11 +63,11 @@ impl Response {
let form = form.clone();
move |_, _| {
control.update(
form.widget.text().is_empty(),
form.text().is_empty(),
size_limit.map(|limit| {
limit as isize
- ((base.to_string_partial(UriHideFlags::QUERY).len()
+ Uri::escape_string(&form.widget.text(), None, false).len())
+ Uri::escape_string(&form.text(), None, false).len())
as isize)
}),
)
Expand All @@ -71,14 +81,14 @@ impl Response {
Some(&format!(
"{}?{}",
base.to_string_partial(UriHideFlags::QUERY),
Uri::escape_string(&form.widget.text(), None, false),
Uri::escape_string(&form.text(), None, false),
)),
true,
);
}
});

// Return activated struct
Self { widget }
Self { g_box }
}
}
21 changes: 14 additions & 7 deletions src/app/browser/window/tab/item/page/input/response/control.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
mod counter;
mod send;
mod widget;

use counter::Counter;
use send::Send;
use widget::Widget;

use gtk::gio::SimpleAction;
use gtk::{gio::SimpleAction, prelude::BoxExt, Align, Box, Orientation};
use std::rc::Rc;

const SPACING: i32 = 8;

pub struct Control {
pub counter: Rc<Counter>,
pub send: Rc<Send>,
pub widget: Rc<Widget>,
pub g_box: Box,
}

impl Control {
Expand All @@ -24,14 +24,21 @@ impl Control {
let counter = Rc::new(Counter::new());
let send = Rc::new(Send::build(action_send));

// Init widget
let widget = Rc::new(Widget::build(&counter.label, &send.button));
// Init main widget
let g_box = Box::builder()
.halign(Align::End)
.orientation(Orientation::Horizontal)
.spacing(SPACING)
.build();

g_box.append(&counter.label);
g_box.append(&send.button);

// Return activated struct
Self {
counter,
send,
widget,
g_box,
}
}

Expand Down

This file was deleted.

60 changes: 51 additions & 9 deletions src/app/browser/window/tab/item/page/input/response/form.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,63 @@
mod widget;
use gtk::{
gio::SimpleAction,
glib::GString,
prelude::{ActionExt, TextBufferExt, TextViewExt, WidgetExt},
TextView, WrapMode,
};
use libspelling::{Checker, TextBufferAdapter};
use sourceview::Buffer;

use widget::Widget;

use gtk::gio::SimpleAction;
use std::rc::Rc;
const MARGIN: i32 = 8;

pub struct Form {
pub widget: Rc<Widget>,
pub text_view: TextView,
}

impl Form {
// Constructors

/// Build new `Self`
pub fn build(action_update: SimpleAction) -> Self {
Self {
widget: Rc::new(Widget::build(action_update)),
}
// Init [SourceView](https://gitlab.gnome.org/GNOME/gtksourceview) type buffer
let buffer = Buffer::builder().build();

// Init [libspelling](https://gitlab.gnome.org/GNOME/libspelling)
let checker = Checker::default();
let adapter = TextBufferAdapter::new(&buffer, &checker);
adapter.set_enabled(true);

// Init main widget
let text_view = TextView::builder()
.bottom_margin(MARGIN)
.buffer(&buffer)
.css_classes(["frame", "view"])
.extra_menu(&adapter.menu_model())
.left_margin(MARGIN)
.margin_bottom(MARGIN / 4)
.right_margin(MARGIN)
.top_margin(MARGIN)
.wrap_mode(WrapMode::Word)
.build();

text_view.insert_action_group("spelling", Some(&adapter));

// Init events
text_view.buffer().connect_changed(move |_| {
action_update.activate(None);
});

text_view.connect_realize(move |this| {
this.grab_focus();
});

// Return activated `Self`
Self { text_view }
}

// Getters

pub fn text(&self) -> GString {
let buffer = self.text_view.buffer();
buffer.text(&buffer.start_iter(), &buffer.end_iter(), true)
}
}
63 changes: 0 additions & 63 deletions src/app/browser/window/tab/item/page/input/response/form/widget.rs

This file was deleted.

Loading

0 comments on commit 972fa6c

Please sign in to comment.