Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a list item factory instead of a set of ListBoxes #3

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions data/resources.gresource.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<gresources>
<gresource prefix="/org/itzswirlz/stox/">
<file compressed="true" preprocess="xml-stripblanks">resources/ui/menu.ui</file>
<file compressed="true" preprocess="xml-stripblanks">resources/ui/stoxsidebaritem.ui</file>
</gresource>
</gresources>
11 changes: 11 additions & 0 deletions data/resources/ui/menu.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0"?>
<interface>
<menu id='app-menu'>
<section>
<item>
<attribute name='label' translatable='yes'>About Stox</attribute>
<attribute name='action'>app.about</attribute>
</item>
</section>
</menu>
</interface>
2 changes: 1 addition & 1 deletion data/resources/ui/stoxsidebaritem.ui
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<property name="can-focus">false</property>
<child>
<object class="GtkGrid">
<property name="margin-start">10</property>
<property name="margin-start">8</property>
<property name="margin-end">10</property>
<property name="margin-top">10</property>
<property name="margin-bottom">10</property>
Expand Down
34 changes: 29 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,18 +198,41 @@ fn build_ui(app: &Application, default_symbol: Option<String>) {
let sidebar = ListBox::new();
sidebar.set_height_request(800);
sidebar.append(&searchbar_row);
if sidebar_show_separators {
sidebar.set_show_separators(true);
}

let sidebar_symbols: Arc<Mutex<Vec<StoxSidebarItem>>> = Arc::new(Mutex::new(Vec::new()));
for symbol in &*saved_stocks.borrow() {
let sidebar_item = StoxSidebarItem::new(symbol, false);
sidebar_item.show();
sidebar.append(&sidebar_item);
sidebar_symbols.lock().unwrap().push(sidebar_item);
}

let model = gio::ListStore::new(StoxSidebarItem::static_type());
model.extend_from_slice(&sidebar_symbols.lock().unwrap());

let factory = SignalListItemFactory::new();
factory.connect_setup(move |_, list_item| {
list_item
.downcast_ref::<ListItem>()
.expect("Needs to be ListItem");
});

factory.connect_bind(move |_, list_item| {
let sidebar_item = list_item
.downcast_ref::<ListItem>()
.expect("Needs to be ListItem")
.item()
.and_downcast::<StoxSidebarItem>()
.expect("The item has to be a StoxSidebarItem.");
list_item.set_child(Some(&sidebar_item));
});

let selection_model = SingleSelection::new(Some(model));
let list_view = ListView::new(Some(selection_model), Some(factory));
sidebar.append(&list_view);

if sidebar_show_separators {
list_view.set_show_separators(true);
}

let (debounce_sender, debounce_receiver) = glib::MainContext::channel(glib::PRIORITY_DEFAULT);

let debounce_source_id = Arc::new(Mutex::new(None::<SourceId>));
Expand Down Expand Up @@ -281,6 +304,7 @@ fn build_ui(app: &Application, default_symbol: Option<String>) {
let scroll_window = ScrolledWindow::builder()
.child(&viewport)
.halign(Align::Center)
.hscrollbar_policy(PolicyType::Never)
.focusable(true)
.min_content_width(325)
.max_content_width(325)
Expand Down