draft find widget

This commit is contained in:
yggverse 2024-12-15 13:08:57 +02:00
parent 02df33428e
commit db44fff212
2 changed files with 91 additions and 3 deletions

View File

@ -1,21 +1,33 @@
mod find;
use find::Find;
use gtk::{
prelude::WidgetExt, EventControllerMotion, GestureClick, TextBuffer, TextView, WrapMode,
prelude::{TextViewExt, WidgetExt},
EventControllerMotion, GestureClick, TextBuffer, TextView, TextWindowType, WrapMode,
};
const MARGIN: i32 = 8;
pub struct Widget {
find: Find,
pub text_view: TextView,
}
impl Widget {
// Construct
// Constructors
/// Create new `Self`
pub fn new(
buffer: &TextBuffer,
primary_button_controller: GestureClick,
middle_button_controller: GestureClick,
motion_controller: EventControllerMotion,
) -> Self {
// Init components
let find = Find::new();
// Init main widget
let text_view = TextView::builder()
.bottom_margin(MARGIN)
.buffer(buffer)
@ -32,6 +44,20 @@ impl Widget {
text_view.add_controller(middle_button_controller);
text_view.add_controller(motion_controller);
Self { text_view }
// Done
Self { find, text_view }
}
// Actions
pub fn find(&self, is_visible: bool) {
if is_visible {
self.text_view
.set_gutter(TextWindowType::Bottom, Some(&self.find.g_box));
self.find.g_box.grab_focus();
} else {
self.text_view
.set_gutter(TextWindowType::Bottom, gtk::Widget::NONE);
}
}
}

View File

@ -0,0 +1,62 @@
use gtk::{
gdk::Cursor,
prelude::{BoxExt, EditableExt, EntryExt},
Box, Button, Entry, EntryIconPosition, Orientation,
};
const MARGIN: i32 = 6;
pub struct Find {
pub g_box: Box,
}
impl Find {
// Construct
pub fn new() -> Self {
// Init components
let close = Button::builder()
.cursor(&Cursor::from_name("default", None).unwrap())
.icon_name("window-close-symbolic")
.margin_bottom(MARGIN)
.margin_end(MARGIN)
.margin_top(MARGIN)
.tooltip_text("Close find bar")
.build();
let entry = Entry::builder()
.hexpand(true)
.margin_bottom(MARGIN)
.margin_end(MARGIN)
.margin_start(MARGIN)
.margin_top(MARGIN)
.placeholder_text("Find in text..")
.primary_icon_activatable(false)
.primary_icon_name("system-search-symbolic")
.build();
// Init main container
let g_box = Box::builder().orientation(Orientation::Horizontal).build();
g_box.append(&entry);
g_box.append(&close);
// Connect events
entry.connect_activate(|_| {}); // @TODO
entry.connect_changed(move |this| {
if this.text().is_empty() {
this.set_secondary_icon_name(None);
} else {
this.set_secondary_icon_name(Some("edit-clear-symbolic"));
}
});
entry.connect_icon_release(move |this, position| match position {
EntryIconPosition::Secondary => this.delete_text(0, -1),
_ => todo!(), // unexpected
});
// Done
Self { g_box }
}
}