Skip to content

Commit

Permalink
fix #42: use gtk's text truncation system when possible (#1066)
Browse files Browse the repository at this point in the history
* fix #42: use gtk's text truncation system when possible

* enhance doc + add to changelog
  • Loading branch information
Rayzeq authored Apr 14, 2024
1 parent ebe5f34 commit 1e37f53
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ All notable changes to eww will be listed here, starting at changes since versio
- The `shell-completions` subcommand is now run before anything is set up
- Fix nix flake
- Fix `jq` (By: w-lfchen)
- Labels now use gtk's truncation system (By: Rayzeq).

### Features
- Add `systray` widget (By: ralismark)
Expand Down
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ notifier_host.workspace = true

gtk = "0.17.1"
gdk = "0.17.1"
pango = "0.17.1"
glib = "0.17.8"
glib-macros = "0.17.8"

Expand Down
62 changes: 47 additions & 15 deletions crates/eww/src/widgets/widget_definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -896,35 +896,67 @@ fn build_gtk_label(bargs: &mut BuilderArgs) -> Result<gtk::Label> {
// @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 show-truncated - show whether the text was truncated. Disabling it will also disable dynamic truncation (the labels won't be truncated more than `limit-width`, even if there is not enough space for them), and will completly disable truncation on pango markup.
// @prop unindent - whether to remove leading spaces
prop(text: as_string, limit_width: as_i32 = i32::MAX, truncate_left: as_bool = false, show_truncated: as_bool = true, unindent: 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()
let text = if show_truncated {
// gtk does weird thing if we set max_width_chars to i32::MAX
if limit_width == i32::MAX {
gtk_widget.set_max_width_chars(-1);
} else {
text.chars().take(limit_width).collect()
};
if show_truncated {
gtk_widget.set_max_width_chars(limit_width);
}
if truncate_left {
gtk_widget.set_ellipsize(pango::EllipsizeMode::Start);
} else {
gtk_widget.set_ellipsize(pango::EllipsizeMode::End);
}

text
} else {
gtk_widget.set_ellipsize(pango::EllipsizeMode::None);

let limit_width = limit_width as usize;
let char_count = text.chars().count();
if char_count > limit_width && !show_truncated {
if truncate_left {
truncated.insert_str(0, "...");
text.chars().skip(char_count - limit_width).collect()
} else {
truncated.push_str("...");
text.chars().take(limit_width).collect()
}
} else {
text
}
truncated
} else {
text
};

let text = unescape::unescape(&text).context(format!("Failed to unescape label text {}", &text))?;
let text = if unindent { util::unindent(&text) } else { 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 truncate-left - whether to truncate on the left side
// @prop show-truncated - show whether the text was truncatedd. Disabling it will also disable dynamic truncation (the labels won't be truncated more than `limit-width`, even if there is not enough space for them), and will completly disable truncation on pango markup.
prop(markup: as_string, limit_width: as_i32 = i32::MAX, truncate_left: as_bool = false, show_truncated: as_bool = true) {
if show_truncated {
// gtk does weird thing if we set max_width_chars to i32::MAX
if limit_width == i32::MAX {
gtk_widget.set_max_width_chars(-1);
} else {
gtk_widget.set_max_width_chars(limit_width);
}

if truncate_left {
gtk_widget.set_ellipsize(pango::EllipsizeMode::Start);
} else {
gtk_widget.set_ellipsize(pango::EllipsizeMode::End);
}
} else {
gtk_widget.set_ellipsize(pango::EllipsizeMode::None);
}

gtk_widget.set_markup(&markup);
},
// @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 1e37f53

Please sign in to comment.