Skip to content

Commit

Permalink
fix elkowar#42: make use of set_max_width_chars for better truncation
Browse files Browse the repository at this point in the history
  • Loading branch information
Rayzeq committed Jan 10, 2024
1 parent 65d622c commit d686b62
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 24 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/eww/Cargo.toml
Expand Up @@ -18,6 +18,7 @@ wayland = ["gtk-layer-shell"]
[dependencies]
gtk = "0.17.1"
gdk = "0.17.1"
pango = "0.17.1"
glib = "0.17.8"
glib-macros = "0.17.8"

Expand Down
37 changes: 13 additions & 24 deletions crates/eww/src/widgets/widget_definitions.rs
Expand Up @@ -825,36 +825,25 @@ fn build_gtk_label(bargs: &mut BuilderArgs) -> Result<gtk::Label> {

def_widget!(bargs, _g, gtk_widget, {
// @prop text - the text to display
// @prop limit-width - maximum count of characters to display
// @prop truncate-left - whether to truncate on the left side
// @prop show-truncated - show whether the text was truncated
prop(text: as_string, limit_width: as_i32 = i32::MAX, truncate_left: as_bool = false, show_truncated: as_bool = true) {
let limit_width = limit_width as usize;
let char_count = text.chars().count();
let text = if char_count > limit_width {
let mut truncated: String = if truncate_left {
text.chars().skip(char_count - limit_width).collect()
} else {
text.chars().take(limit_width).collect()
};
if show_truncated {
if truncate_left {
truncated.insert_str(0, "...");
} else {
truncated.push_str("...");
}
}
truncated
} else {
text
};

prop(text: as_string) {
let text = unescape::unescape(&text).context(format!("Failed to unescape label text {}", &text))?;
let text = unindent(&text);
gtk_widget.set_text(&text);
},
// @prop markup - Pango markup to display
prop(markup: as_string) { gtk_widget.set_markup(&markup); },
// @prop limit-width - maximum count of characters to display
prop(limit_width: as_i32) {
gtk_widget.set_max_width_chars(limit_width);
},
// @prop truncate-left - whether to truncate on the left side
prop(truncate_left: as_bool = false) {
if truncate_left {
gtk_widget.set_ellipsize(pango::EllipsizeMode::Start);
} else {
gtk_widget.set_ellipsize(pango::EllipsizeMode::End);
}
},
// @prop wrap - Wrap the text. This mainly makes sense if you set the width of this widget.
prop(wrap: as_bool) { gtk_widget.set_line_wrap(wrap) },
// @prop angle - the angle of rotation for the label (between 0 - 360)
Expand Down

0 comments on commit d686b62

Please sign in to comment.