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

port css and entry_completion from gtk-rs/examples4 #93

Merged
merged 6 commits into from Dec 2, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions examples/Cargo.toml
Expand Up @@ -13,6 +13,10 @@ chrono = "0.4.19"
path = "../gtk4"
package = "gtk4"

[dependencies.gdk]
path = "../gdk4"
package = "gdk4"

[dependencies.gio]
git = "https://github.com/gtk-rs/gtk-rs"

Expand All @@ -33,3 +37,9 @@ name = "clock"

[[bin]]
name = "composite_template"

[[bin]]
name = "css"

[[bin]]
name = "entry_completion"
97 changes: 97 additions & 0 deletions examples/src/bin/css.rs
@@ -0,0 +1,97 @@
//! # CSS example
//!
//! This sample demonstrates how to use CSS with gtk-rs.

use gio::prelude::*;
use gtk::prelude::*;

use std::env::args;

use gdk::Display;
use gtk::{
Application, ApplicationWindow, Box as Box_, Button, ComboBoxText, CssProvider, Entry,
Orientation, StyleContext, STYLE_PROVIDER_PRIORITY_APPLICATION,
};
// Basic CSS: we change background color, we set font color to black and we set it as bold.
const STYLE: &str = "
entry.entry1 {
background: linear-gradient(to right, #f00, #0f0);
color: blue;
font-weight: bold;
}

button {
/* If we don't put it, the yellow background won't be visible */
background-image: none;
}

button.button1:hover {
transition: 500ms;
color: red;
background-color: yellow;
}

combobox button.combo box {
padding: 5px;
}
combobox box arrow {
-gtk-icon-source: -gtk-icontheme('pan-down-symbolic');
bilelmoussaoui marked this conversation as resolved.
Show resolved Hide resolved
border-left: 5px solid transparent;
border-right: 5px solid transparent;
border-top: 5px solid black;
}";

fn build_ui(application: &Application) {
let window = ApplicationWindow::new(application);

window.set_title("CSS");

// The container container.
let vbox = Box_::new(Orientation::Vertical, 0);

let button = Button::with_label("hover me!");
button.add_css_class("button1");

let entry = Entry::new();
entry.add_css_class("entry1");
entry.set_text("Some text");

let combo = ComboBoxText::new();
combo.append_text("option 1");
combo.append_text("option 2");
combo.append_text("option 3");
combo.set_active(Some(0));

vbox.append(&button);
vbox.append(&entry);
vbox.append(&combo);
// Then we add the container inside our window.
window.set_child(Some(&vbox));

application.connect_activate(move |_| {
window.show();
});
}

fn main() {
let application = Application::new(Some("com.github.css"), gio::ApplicationFlags::empty())
.expect("Initialization failed...");

application.connect_startup(|app| {
// The CSS "magic" happens here.
let provider = CssProvider::new();
provider.load_from_data(STYLE.as_bytes());
// We give the CssProvided to the default screen so the CSS rules we added
// can be applied to our window.
StyleContext::add_provider_for_display(
&Display::get_default().expect("Error initializing gtk css provider."),
&provider,
STYLE_PROVIDER_PRIORITY_APPLICATION,
);

// We build the application UI.
build_ui(app);
});

application.run(&args().collect::<Vec<_>>());
}
109 changes: 109 additions & 0 deletions examples/src/bin/entry_completion.rs
@@ -0,0 +1,109 @@
//! # Entry completion example
//!
//! This example demonstrates how to build a list of items and use them
//! to autocomplete a field as the user types in something.

use gio::prelude::*;
use gtk::prelude::*;

use std::env::args;

use gio::SimpleAction;
use glib::Type;
use gtk::{
Application, ApplicationWindow, Box, Entry, EntryCompletion, Label, ListStore, Orientation,
};

struct Data {
description: String,
}

fn create_list_model() -> ListStore {
let col_types: [Type; 1] = [Type::String];

let data: [Data; 4] = [
Data {
description: "France".to_string(),
},
Data {
description: "Italy".to_string(),
},
Data {
description: "Sweden".to_string(),
},
Data {
description: "Switzerland".to_string(),
},
];
let store = ListStore::new(&col_types);
let col_indices: [u32; 1] = [0];
for d in data.iter() {
let values: [&dyn ToValue; 1] = [&d.description];
store.set(&store.append(), &col_indices, &values);
}
store
}

fn build_ui(application: &Application) {
// create the main window
let window = ApplicationWindow::new(application);
window.set_title("Entry with autocompletion");
window.set_default_size(840, 480);

// Create a title label
let win_title = Label::new(None);
win_title.set_markup("<big>Which country would you like to spend a holiday in?</big>");

// Create an EntryCompletion widget
let completion_countries = EntryCompletion::new();
// Use the first (and only) column available to set the autocompletion text
completion_countries.set_text_column(0);
// how many keystrokes to wait before attempting to autocomplete?
completion_countries.set_minimum_key_length(1);
// whether the completions should be presented in a popup window
completion_countries.set_popup_completion(true);

// Create a ListStore of items
// These will be the source for the autocompletion
// as the user types into the field
// For a more evolved example of ListStore see src/bin/list_store.rs
let ls = create_list_model();
completion_countries.set_model(Some(&ls));
bilelmoussaoui marked this conversation as resolved.
Show resolved Hide resolved

let input_field = Entry::new();
input_field.set_completion(Some(&completion_countries));

let row = Box::new(Orientation::Vertical, 5);
row.append(&win_title);
input_field.set_margin_top(10);
row.append(&input_field);

window.set_child(Some(&row));

// show everything
window.show();
}

fn main() {
let application = Application::new(
Some("com.github.gtk-rs.examples.entry-completion"),
Default::default(),
)
.expect("Initialization failed...");
application.connect_activate(|app| {
build_ui(app);
});

// When activated, shuts down the application
let quit = SimpleAction::new("quit", None);
quit.connect_activate(
glib::clone!(@weak application => move |_action, _parameter| {
application.quit();
}),
);
application.set_accels_for_action("app.quit", &["<Primary>Q"]);
application.add_action(&quit);

// Run the application
application.run(&args().collect::<Vec<_>>());
}