move actions connect into the search component

This commit is contained in:
yggverse 2024-12-17 02:52:21 +02:00
parent 8b65df99f4
commit dd5d79bac4
6 changed files with 125 additions and 129 deletions

View File

@ -64,11 +64,7 @@ impl Page {
), ),
) -> Self { ) -> Self {
// Init components // Init components
let content = Rc::new(Content::new(( let content = Rc::new(Content::new((window_action.clone(), tab_action.clone())));
browser_action.clone(),
window_action.clone(),
tab_action.clone(),
)));
let search = Rc::new(Search::new()); let search = Rc::new(Search::new());

View File

@ -5,7 +5,7 @@ mod text;
use image::Image; use image::Image;
use text::Text; use text::Text;
use super::{BrowserAction, TabAction, WindowAction}; use super::{TabAction, WindowAction};
use adw::StatusPage; use adw::StatusPage;
use gtk::{ use gtk::{
gdk::Paintable, gdk::Paintable,
@ -17,7 +17,6 @@ use gtk::{
use std::{rc::Rc, time::Duration}; use std::{rc::Rc, time::Duration};
pub struct Content { pub struct Content {
browser_action: Rc<BrowserAction>,
window_action: Rc<WindowAction>, window_action: Rc<WindowAction>,
tab_action: Rc<TabAction>, tab_action: Rc<TabAction>,
pub g_box: Box, pub g_box: Box,
@ -27,16 +26,9 @@ impl Content {
// Construct // Construct
/// Create new container for different components /// Create new container for different components
pub fn new( pub fn new((window_action, tab_action): (Rc<WindowAction>, Rc<TabAction>)) -> Self {
(browser_action, window_action, tab_action): (
Rc<BrowserAction>,
Rc<WindowAction>,
Rc<TabAction>,
),
) -> Self {
Self { Self {
g_box: Box::builder().orientation(Orientation::Vertical).build(), g_box: Box::builder().orientation(Orientation::Vertical).build(),
browser_action,
window_action, window_action,
tab_action, tab_action,
} }
@ -130,11 +122,7 @@ impl Content {
/// * could be useful to extract document title parsed from Gemtext /// * could be useful to extract document title parsed from Gemtext
pub fn to_text_gemini(&self, base: &Uri, data: &str) -> Text { pub fn to_text_gemini(&self, base: &Uri, data: &str) -> Text {
self.clean(); self.clean();
let text = Text::new_gemini( let text = Text::new_gemini(data, base, (&self.window_action, &self.tab_action));
data,
base,
(&self.browser_action, &self.window_action, &self.tab_action),
);
self.g_box.append(&text.g_box); self.g_box.append(&text.g_box);
text text
} }

View File

@ -4,7 +4,7 @@ mod source;
use gemini::Gemini; use gemini::Gemini;
use source::Source; use source::Source;
use super::{BrowserAction, TabAction, WindowAction}; use super::{TabAction, WindowAction};
use gtk::{ use gtk::{
glib::Uri, glib::Uri,
prelude::{BoxExt, Cast}, prelude::{BoxExt, Cast},
@ -28,11 +28,7 @@ impl Text {
pub fn new_gemini( pub fn new_gemini(
gemtext: &str, gemtext: &str,
base: &Uri, base: &Uri,
(browser_action, window_action, tab_action): ( (window_action, tab_action): (&Rc<WindowAction>, &Rc<TabAction>),
&Rc<BrowserAction>,
&Rc<WindowAction>,
&Rc<TabAction>,
),
) -> Self { ) -> Self {
// Init components // Init components
let gemini = Gemini::new(gemtext, base, (window_action, tab_action)); let gemini = Gemini::new(gemtext, base, (window_action, tab_action));
@ -46,50 +42,6 @@ impl Text {
.build(), .build(),
); );
// Connect events
/* @TODO
browser_action.escape.connect_activate({
let close = search.close.clone();
move || {
close.activate();
}
});
window_action.find.connect_activate({
let search = search.clone();
move |_| {
search.g_box.set_visible(true);
search.input.entry.grab_focus();
}
});
search.navigation.back.button.connect_clicked({
let text_view = gemini.reader.widget.text_view.clone();
let navigation = search.navigation.clone();
move |_| {
if let Some((mut start, _)) = navigation.back() {
text_view.scroll_to_iter(&mut start, 0.0, true, 0.0, 0.0);
}
}
});
search.navigation.forward.button.connect_clicked({
let text_view = gemini.reader.widget.text_view.clone();
let navigation = search.navigation.clone();
move |_| {
if let Some((mut start, _)) = navigation.forward() {
text_view.scroll_to_iter(&mut start, 0.0, true, 0.0, 0.0);
}
}
});
search.close.connect_clicked({
let search = search.clone();
move |_| {
search.g_box.set_visible(false);
}
});*/
Self { Self {
text_view: gemini.reader.widget.text_view.clone().upcast::<TextView>(), text_view: gemini.reader.widget.text_view.clone().upcast::<TextView>(),
meta: Meta { meta: Meta {

View File

@ -7,8 +7,8 @@ use placeholder::Placeholder;
use subject::Subject; use subject::Subject;
use gtk::{ use gtk::{
prelude::{BoxExt, WidgetExt}, prelude::{BoxExt, ButtonExt, WidgetExt},
Align, Box, Orientation, TextBuffer, TextView, Align, Box, Orientation, TextView,
}; };
use std::{cell::RefCell, rc::Rc}; use std::{cell::RefCell, rc::Rc};
@ -40,6 +40,13 @@ impl Search {
g_box.append(&form.g_box); g_box.append(&form.g_box);
g_box.append(&placeholder.label); g_box.append(&placeholder.label);
// Connect events
form.close.connect_clicked({
let g_box = g_box.clone();
move |_| g_box.set_visible(false)
});
// Done // Done
Self { Self {
subject, subject,
@ -51,6 +58,10 @@ impl Search {
// Actions // Actions
pub fn escape(&self) {
self.hide()
}
pub fn show(&self) { pub fn show(&self) {
if self.subject.borrow().is_some() { if self.subject.borrow().is_some() {
self.form.show(); self.form.show();

View File

@ -11,11 +11,13 @@ use gtk::{
prelude::{ prelude::{
BoxExt, ButtonExt, CheckButtonExt, EditableExt, TextBufferExt, TextViewExt, WidgetExt, BoxExt, ButtonExt, CheckButtonExt, EditableExt, TextBufferExt, TextViewExt, WidgetExt,
}, },
Align, Box, Orientation, TextIter, TextSearchFlags, Align, Box, Button, Orientation, TextIter, TextSearchFlags,
}; };
use std::{cell::RefCell, rc::Rc}; use std::{cell::RefCell, rc::Rc};
pub struct Form { pub struct Form {
pub close: Button,
pub input: Rc<Input>,
pub g_box: Box, pub g_box: Box,
} }
@ -23,7 +25,7 @@ impl Form {
// Constructors // Constructors
/// Create new `Self` /// Create new `Self`
pub fn new(buffer: &Rc<RefCell<Option<Subject>>>) -> Self { pub fn new(subject: &Rc<RefCell<Option<Subject>>>) -> Self {
// Init components // Init components
let close = close::new(); let close = close::new();
let input = Rc::new(Input::new()); let input = Rc::new(Input::new());
@ -53,10 +55,10 @@ impl Form {
let input = input.clone(); let input = input.clone();
let match_case = match_case.clone(); let match_case = match_case.clone();
let navigation = navigation.clone(); let navigation = navigation.clone();
let buffer = buffer.clone(); let subject = subject.clone();
move |_| { move |_| {
navigation.update(find( navigation.update(find(
&buffer, &subject,
input.entry.text().as_str(), input.entry.text().as_str(),
match_case.is_active(), match_case.is_active(),
)); ));
@ -67,22 +69,63 @@ impl Form {
match_case.connect_toggled({ match_case.connect_toggled({
let input = input.clone(); let input = input.clone();
let navigation = navigation.clone(); let navigation = navigation.clone();
let buffer = buffer.clone(); let subject = subject.clone();
move |this| { move |this| {
navigation.update(find(&buffer, input.entry.text().as_str(), this.is_active())); navigation.update(find(
&subject,
input.entry.text().as_str(),
this.is_active(),
));
input.update(navigation.is_match()); input.update(navigation.is_match());
} }
}); });
// Connect events
navigation.back.button.connect_clicked({
let subject = subject.clone();
let navigation = navigation.clone();
move |_| match subject.borrow().as_ref() {
Some(subject) => match navigation.back(subject) {
Some((mut start, _)) => {
subject
.text_view
.scroll_to_iter(&mut start, 0.0, true, 0.0, 0.0);
}
None => todo!(),
},
None => todo!(),
}
});
navigation.forward.button.connect_clicked({
let subject = subject.clone();
let navigation = navigation.clone();
move |_| match subject.borrow().as_ref() {
Some(subject) => match navigation.forward(subject) {
Some((mut start, _)) => {
subject
.text_view
.scroll_to_iter(&mut start, 0.0, true, 0.0, 0.0);
}
None => todo!(),
},
None => todo!(),
}
});
// Done // Done
Self { g_box } Self {
close,
g_box,
input,
}
} }
// Actions // Actions
pub fn show(&self) { pub fn show(&self) {
//self.buffer.get_mut().is_none() self.g_box.set_visible(true);
self.g_box.set_visible(true) self.input.entry.grab_focus();
} }
pub fn hide(&self) { pub fn hide(&self) {

View File

@ -4,9 +4,10 @@ mod forward;
use back::Back; use back::Back;
use forward::Forward; use forward::Forward;
use super::Subject;
use gtk::{ use gtk::{
prelude::{BoxExt, TextBufferExt}, prelude::{BoxExt, TextBufferExt, TextViewExt},
Box, Orientation, TextBuffer, TextIter, TextTag, Box, Orientation, TextIter,
}; };
use std::{ use std::{
cell::{Cell, RefCell}, cell::{Cell, RefCell},
@ -68,55 +69,60 @@ impl Navigation {
self.back.update(self.is_match()); self.back.update(self.is_match());
self.forward.update(self.is_match()); self.forward.update(self.is_match());
} }
/*
pub fn back(&self) -> Option<(TextIter, TextIter)> {
self.text_buffer.remove_tag(
&self.current_tag,
&self.text_buffer.start_iter(),
&self.text_buffer.end_iter(),
);
let index = self.index.take(); pub fn back(&self, subject: &Subject) -> Option<(TextIter, TextIter)> {
match self.matches.borrow().get(back(index)) { let buffer = subject.text_view.buffer();
Some((start, end)) => {
self.text_buffer.apply_tag(&self.current_tag, start, end);
self.index.replace(if index == 0 {
len_to_index(self.matches.borrow().len())
} else {
index
});
Some((*start, *end))
}
None => {
self.index
.replace(len_to_index(self.matches.borrow().len())); // go last
None
}
}
}
pub fn forward(&self) -> Option<(TextIter, TextIter)> { buffer.remove_tag(
self.text_buffer.remove_tag( &subject.tag.current,
&self.current_tag, &buffer.start_iter(),
&self.text_buffer.start_iter(), &buffer.end_iter(),
&self.text_buffer.end_iter(), );
);
let index = self.index.take();
match self.matches.borrow().get(back(index)) {
Some((start, end)) => {
buffer.apply_tag(&subject.tag.current, start, end);
self.index.replace(if index == 0 {
len_to_index(self.matches.borrow().len())
} else {
index
});
Some((*start, *end))
}
None => {
self.index
.replace(len_to_index(self.matches.borrow().len())); // go last
None
}
}
}
pub fn forward(&self, subject: &Subject) -> Option<(TextIter, TextIter)> {
let buffer = subject.text_view.buffer();
buffer.remove_tag(
&subject.tag.current,
&buffer.start_iter(),
&buffer.end_iter(),
);
let index = self.index.take();
let next = forward(index);
match self.matches.borrow().get(next) {
Some((start, end)) => {
buffer.apply_tag(&subject.tag.current, start, end);
self.index.replace(next);
Some((*start, *end))
}
None => {
self.index.replace(0);
None
}
}
}
let index = self.index.take();
let next = forward(index);
match self.matches.borrow().get(next) {
Some((start, end)) => {
self.text_buffer.apply_tag(&self.current_tag, start, end);
self.index.replace(next);
Some((*start, *end))
}
None => {
self.index.replace(0);
None
}
}
}
*/
// Getters // Getters
pub fn is_match(&self) -> bool { pub fn is_match(&self) -> bool {