Skip to content

Commit

Permalink
chore: update (#38)
Browse files Browse the repository at this point in the history
simplify the source code and make fix some mistakes
  • Loading branch information
pythonbrad authored Nov 30, 2023
1 parent 40a0fc3 commit 60290e2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 68 deletions.
102 changes: 35 additions & 67 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ pub use crate::config::Config;
pub struct Wish {
border: f64,
window: rstk::TkTopLevel,
theme: HashMap<&'static str, Style>,
themes: HashMap<&'static str, Style>,
predicates: Vec<(String, String, String)>,
page_size: usize,
current_predicate_id: usize,
input: String,
cursor_widget: rstk::TkLabel,
predicates_widget: rstk::TkLabel,
}

impl Wish {
Expand All @@ -28,52 +30,38 @@ impl Wish {

init_rstk_ext();

let mut theme = HashMap::new();
let mut themes = HashMap::new();

if let Some(theme_config) = config.theme {
let header_frame_style = Style {
name: "header.TFrame",
background: theme_config.header.background.to_owned(),
..Default::default()
};
header_frame_style.update();
theme.insert("HFrame", header_frame_style);

let header_label_style = Style {
name: "header.TLabel",
let style = Style {
name: "header.predicates.TLabel",
background: theme_config.header.background,
foreground: theme_config.header.foreground,
font_size: theme_config.header.font.size,
font_family: theme_config.header.font.family,
font_weight: theme_config.header.font.weight,
};
header_label_style.update();
theme.insert("HLabel", header_label_style);

let body_frame_style = Style {
name: "body.TFrame",
background: theme_config.body.background.to_owned(),
..Default::default()
};
body_frame_style.update();
theme.insert("BFrame", body_frame_style);
style.update();
themes.insert("PHLabel", style);

let body_label_style = Style {
name: "body.TLabel",
let style = Style {
name: "body.predicates.TLabel",
background: theme_config.body.background,
foreground: theme_config.body.foreground,
font_size: theme_config.body.font.size,
font_family: theme_config.body.font.family,
font_family: theme_config.body.font.family.to_owned(),
font_weight: theme_config.body.font.weight,
};
body_label_style.update();
theme.insert("BLabel", body_label_style);
style.update();
themes.insert("PBLabel", style);
};

Wish {
predicates_widget: rstk::make_label(&wish),
cursor_widget: rstk::make_label(&wish),
window: wish,
border: 0.0,
theme,
themes,
predicates: Vec::new(),
page_size: 10,
current_predicate_id: 0,
Expand All @@ -90,9 +78,16 @@ impl Wish {
self.window.topmost(true);
self.window.deiconify();

let label = rstk::make_label(&self.window);
label.text("Type _exit_ to end the clafrica");
label.pack().layout();
// Cursor
self.cursor_widget = rstk::make_label(&self.window);
self.cursor_widget.text("Type _exit_ to end the clafrica");
self.cursor_widget.style(&self.themes["PHLabel"]);
self.cursor_widget.pack().fill(PackFill::X).layout();

// Predication
self.predicates_widget = rstk::make_label(&self.window);
self.predicates_widget.style(&self.themes["PBLabel"]);
self.predicates_widget.pack().fill(PackFill::X).layout();
}
}

Expand Down Expand Up @@ -153,47 +148,20 @@ impl Frontend for Wish {
rstk::end_wish();
}

self.window.clear();
let header_frame = rstk::make_frame(&self.window);

if let Some(v) = self.theme.get("HFrame") {
header_frame.style(v);
}

let label = rstk::make_label(&header_frame);
label.text("Type _exit_ to end the clafrica");

if let Some(v) = self.theme.get("HLabel") {
label.style(v);
}
label.pack().side(PackSide::Left).layout();
header_frame.pack().fill(PackFill::X).layout();
label.text(&self.input);

let prediction_frame = rstk::make_frame(&self.window);
let page_size = std::cmp::min(self.page_size, self.predicates.len());
self.predicates
let texts: Vec<String> = self
.predicates
.iter()
.enumerate()
.chain(self.predicates.iter().enumerate())
.skip(self.current_predicate_id)
.take(page_size)
.for_each(|(i, (_code, remaining_code, text))| {
let frame = rstk::make_frame(&prediction_frame);

if let Some(v) = self.theme.get("BFrame") {
frame.style(v);
}
frame.pack().fill(PackFill::X).layout();

let label = rstk::make_label(&frame);
label.text(&format!("{}. {text} ~{remaining_code}", i + 1,));
if let Some(v) = self.theme.get("BLabel") {
label.style(v);
}
label.pack().side(PackSide::Left).layout();
});

prediction_frame.pack().fill(PackFill::X).layout();
.map(|(i, (_code, remaining_code, text))| {
format!("{}. {text} ~{remaining_code}", i + 1,)
})
.collect();

self.cursor_widget.text(&self.input);
self.predicates_widget.text(&texts.join("\n"));
}
}
5 changes: 4 additions & 1 deletion src/rstk_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ pub struct Style {

impl Style {
pub fn update(&self) {
rstk::tell_wish(&format!("ttk::style layout {} {{}};", self.name));
rstk::tell_wish(&format!(
"ttk::style layout {} [ttk::style layout {}];",
self.name, self.name
));
rstk::tell_wish(&format!(
"ttk::style config {} -background {{{}}} -foreground {{{}}} -font {{{} {} {}}};",
self.name,
Expand Down

0 comments on commit 60290e2

Please sign in to comment.