From dd5d79bac4afc8cf640d7b2ecdfd11086f1f2d4f Mon Sep 17 00:00:00 2001 From: yggverse Date: Tue, 17 Dec 2024 02:52:21 +0200 Subject: [PATCH] move actions connect into the search component --- src/app/browser/window/tab/item/page.rs | 6 +- .../browser/window/tab/item/page/content.rs | 18 +--- .../window/tab/item/page/content/text.rs | 52 +-------- .../browser/window/tab/item/page/search.rs | 15 ++- .../window/tab/item/page/search/form.rs | 61 +++++++++-- .../tab/item/page/search/form/navigation.rs | 102 +++++++++--------- 6 files changed, 125 insertions(+), 129 deletions(-) diff --git a/src/app/browser/window/tab/item/page.rs b/src/app/browser/window/tab/item/page.rs index 8774ae02..29db8440 100644 --- a/src/app/browser/window/tab/item/page.rs +++ b/src/app/browser/window/tab/item/page.rs @@ -64,11 +64,7 @@ impl Page { ), ) -> Self { // Init components - let content = Rc::new(Content::new(( - browser_action.clone(), - window_action.clone(), - tab_action.clone(), - ))); + let content = Rc::new(Content::new((window_action.clone(), tab_action.clone()))); let search = Rc::new(Search::new()); diff --git a/src/app/browser/window/tab/item/page/content.rs b/src/app/browser/window/tab/item/page/content.rs index cfc6c228..29618873 100644 --- a/src/app/browser/window/tab/item/page/content.rs +++ b/src/app/browser/window/tab/item/page/content.rs @@ -5,7 +5,7 @@ mod text; use image::Image; use text::Text; -use super::{BrowserAction, TabAction, WindowAction}; +use super::{TabAction, WindowAction}; use adw::StatusPage; use gtk::{ gdk::Paintable, @@ -17,7 +17,6 @@ use gtk::{ use std::{rc::Rc, time::Duration}; pub struct Content { - browser_action: Rc, window_action: Rc, tab_action: Rc, pub g_box: Box, @@ -27,16 +26,9 @@ impl Content { // Construct /// Create new container for different components - pub fn new( - (browser_action, window_action, tab_action): ( - Rc, - Rc, - Rc, - ), - ) -> Self { + pub fn new((window_action, tab_action): (Rc, Rc)) -> Self { Self { g_box: Box::builder().orientation(Orientation::Vertical).build(), - browser_action, window_action, tab_action, } @@ -130,11 +122,7 @@ impl Content { /// * could be useful to extract document title parsed from Gemtext pub fn to_text_gemini(&self, base: &Uri, data: &str) -> Text { self.clean(); - let text = Text::new_gemini( - data, - base, - (&self.browser_action, &self.window_action, &self.tab_action), - ); + let text = Text::new_gemini(data, base, (&self.window_action, &self.tab_action)); self.g_box.append(&text.g_box); text } diff --git a/src/app/browser/window/tab/item/page/content/text.rs b/src/app/browser/window/tab/item/page/content/text.rs index 15924465..f05f2a16 100644 --- a/src/app/browser/window/tab/item/page/content/text.rs +++ b/src/app/browser/window/tab/item/page/content/text.rs @@ -4,7 +4,7 @@ mod source; use gemini::Gemini; use source::Source; -use super::{BrowserAction, TabAction, WindowAction}; +use super::{TabAction, WindowAction}; use gtk::{ glib::Uri, prelude::{BoxExt, Cast}, @@ -28,11 +28,7 @@ impl Text { pub fn new_gemini( gemtext: &str, base: &Uri, - (browser_action, window_action, tab_action): ( - &Rc, - &Rc, - &Rc, - ), + (window_action, tab_action): (&Rc, &Rc), ) -> Self { // Init components let gemini = Gemini::new(gemtext, base, (window_action, tab_action)); @@ -46,50 +42,6 @@ impl Text { .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 { text_view: gemini.reader.widget.text_view.clone().upcast::(), meta: Meta { diff --git a/src/app/browser/window/tab/item/page/search.rs b/src/app/browser/window/tab/item/page/search.rs index 147f9bef..13baf400 100644 --- a/src/app/browser/window/tab/item/page/search.rs +++ b/src/app/browser/window/tab/item/page/search.rs @@ -7,8 +7,8 @@ use placeholder::Placeholder; use subject::Subject; use gtk::{ - prelude::{BoxExt, WidgetExt}, - Align, Box, Orientation, TextBuffer, TextView, + prelude::{BoxExt, ButtonExt, WidgetExt}, + Align, Box, Orientation, TextView, }; use std::{cell::RefCell, rc::Rc}; @@ -40,6 +40,13 @@ impl Search { g_box.append(&form.g_box); g_box.append(&placeholder.label); + // Connect events + + form.close.connect_clicked({ + let g_box = g_box.clone(); + move |_| g_box.set_visible(false) + }); + // Done Self { subject, @@ -51,6 +58,10 @@ impl Search { // Actions + pub fn escape(&self) { + self.hide() + } + pub fn show(&self) { if self.subject.borrow().is_some() { self.form.show(); diff --git a/src/app/browser/window/tab/item/page/search/form.rs b/src/app/browser/window/tab/item/page/search/form.rs index 373be592..b3cef100 100644 --- a/src/app/browser/window/tab/item/page/search/form.rs +++ b/src/app/browser/window/tab/item/page/search/form.rs @@ -11,11 +11,13 @@ use gtk::{ prelude::{ BoxExt, ButtonExt, CheckButtonExt, EditableExt, TextBufferExt, TextViewExt, WidgetExt, }, - Align, Box, Orientation, TextIter, TextSearchFlags, + Align, Box, Button, Orientation, TextIter, TextSearchFlags, }; use std::{cell::RefCell, rc::Rc}; pub struct Form { + pub close: Button, + pub input: Rc, pub g_box: Box, } @@ -23,7 +25,7 @@ impl Form { // Constructors /// Create new `Self` - pub fn new(buffer: &Rc>>) -> Self { + pub fn new(subject: &Rc>>) -> Self { // Init components let close = close::new(); let input = Rc::new(Input::new()); @@ -53,10 +55,10 @@ impl Form { let input = input.clone(); let match_case = match_case.clone(); let navigation = navigation.clone(); - let buffer = buffer.clone(); + let subject = subject.clone(); move |_| { navigation.update(find( - &buffer, + &subject, input.entry.text().as_str(), match_case.is_active(), )); @@ -67,22 +69,63 @@ impl Form { match_case.connect_toggled({ let input = input.clone(); let navigation = navigation.clone(); - let buffer = buffer.clone(); + let subject = subject.clone(); 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()); } }); + // 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 - Self { g_box } + Self { + close, + g_box, + input, + } } // Actions 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) { diff --git a/src/app/browser/window/tab/item/page/search/form/navigation.rs b/src/app/browser/window/tab/item/page/search/form/navigation.rs index 7b57248b..7d500c0a 100644 --- a/src/app/browser/window/tab/item/page/search/form/navigation.rs +++ b/src/app/browser/window/tab/item/page/search/form/navigation.rs @@ -4,9 +4,10 @@ mod forward; use back::Back; use forward::Forward; +use super::Subject; use gtk::{ - prelude::{BoxExt, TextBufferExt}, - Box, Orientation, TextBuffer, TextIter, TextTag, + prelude::{BoxExt, TextBufferExt, TextViewExt}, + Box, Orientation, TextIter, }; use std::{ cell::{Cell, RefCell}, @@ -68,55 +69,60 @@ impl Navigation { self.back.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(); - match self.matches.borrow().get(back(index)) { - 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 back(&self, subject: &Subject) -> Option<(TextIter, TextIter)> { + let buffer = subject.text_view.buffer(); - pub fn forward(&self) -> Option<(TextIter, TextIter)> { - self.text_buffer.remove_tag( - &self.current_tag, - &self.text_buffer.start_iter(), - &self.text_buffer.end_iter(), - ); + buffer.remove_tag( + &subject.tag.current, + &buffer.start_iter(), + &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 pub fn is_match(&self) -> bool {