Skip to content

Commit

Permalink
Fixing Windows winapi crash on run with SetUnderlineColor (#651)
Browse files Browse the repository at this point in the history
  • Loading branch information
Xithrius authored Aug 19, 2024
1 parent 34e5bba commit 92dc384
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 109 deletions.
18 changes: 13 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ categories = ["command-line-utilities"]

[dependencies]
crossterm = "0.28.1"
tui = { package = "ratatui", version = "0.28.0", default-features = false, features = [
"crossterm",
"serde",
"underline-color",
] }
tokio = { version = "1.39.2", features = [
"rt",
"macros",
Expand Down Expand Up @@ -52,6 +47,19 @@ webbrowser = "1.0.1"
memchr = "2.7.4"
webp-animation = { version = "0.9.0", features = ["image"] }

[target.'cfg(not(windows))'.dependencies]
tui = { package = "ratatui", version = "0.28.0", default-features = false, features = [
"crossterm",
"serde",
"underline-color",
] }

[target.'cfg(windows)'.dependencies]
tui = { package = "ratatui", version = "0.28.0", default-features = false, features = [
"crossterm",
"serde",
] }

[features]
static-webp = ["webp-animation/static"]

Expand Down
13 changes: 12 additions & 1 deletion src/handlers/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,9 +339,19 @@ impl MessageData {
content: impl Into<Cow<'s, str>>,
emotes: &mut &[(Color, Color)],
) -> Span<'s> {
#[allow(unused_variables)]
if let Some(&(id, pid)) = emotes.first() {
*emotes = &emotes[1..];
Span::styled(content, Style::default().fg(id).underline_color(pid))

#[cfg(not(target_os = "windows"))]
{
Span::styled(content, Style::default().fg(id).underline_color(pid))
}

#[cfg(target_os = "windows")]
{
Span::styled(content, Style::default().fg(id))
}
} else {
error!("Emote index >= emotes.len()");
Span::raw(content)
Expand Down Expand Up @@ -655,6 +665,7 @@ impl<'conf> DataBuilder<'conf> {
}

#[cfg(test)]
#[cfg(not(target_os = "windows"))]
mod tests {
use super::*;
use std::collections::BTreeMap;
Expand Down
12 changes: 9 additions & 3 deletions src/ui/components/emote_picker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,14 @@ impl Component for EmotePickerWidget {

let cols = (loaded_emote.width as f32 / cell_size.0).ceil() as u16;

#[cfg(not(target_os = "windows"))]
let underline_style = Style::default()
.fg(u32_to_color(loaded_emote.hash))
.underline_color(u32_to_color(1));

#[cfg(target_os = "windows")]
let underline_style = { Style::default().fg(u32_to_color(loaded_emote.hash)) };

let row = vec![
Span::raw(name[0..pos].to_owned()),
Span::styled(
Expand All @@ -187,9 +195,7 @@ impl Component for EmotePickerWidget {
Span::raw(" - "),
Span::styled(
UnicodePlaceholder::new(cols as usize).string(),
Style::default()
.fg(u32_to_color(loaded_emote.hash))
.underline_color(u32_to_color(1)),
underline_style,
),
];

Expand Down
151 changes: 51 additions & 100 deletions src/utils/styles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,128 +21,79 @@ macro_rules! color {
};
}

pub static BOLD_STYLE: Lazy<Style> = Lazy::new(|| Style {
fg: None,
bg: None,
underline_color: None,
add_modifier: *BOLD,
sub_modifier: Modifier::empty(),
});
macro_rules! define_style {
($name:ident, $($key:ident: $value:expr),*) => {
pub static $name: Lazy<Style> = Lazy::new(|| Style {
$(
$key: $value,
)*
..Style::default()
});
};
}

pub static STATE_TABS_STYLE: Lazy<Style> = Lazy::new(|| Style {
define_style!(
BOLD_STYLE,
add_modifier: *BOLD
);

define_style!(STATE_TABS_STYLE,
fg: color!(Color::Gray),
bg: None,
underline_color: None,
add_modifier: if *NO_COLOR {
Modifier::empty()
} else {
Modifier::DIM
},
sub_modifier: Modifier::empty(),
});

pub static TEXT_DARK_STYLE: Lazy<Style> = Lazy::new(|| Style {
fg: color!(Color::White),
bg: None,
underline_color: None,
add_modifier: Modifier::empty(),
sub_modifier: Modifier::empty(),
});

pub static DASHBOARD_SECTION_STYLE: Lazy<Style> = Lazy::new(|| Style {
fg: color!(Color::LightRed),
bg: None,
underline_color: None,
add_modifier: Modifier::empty(),
sub_modifier: Modifier::empty(),
});
}
);

#[allow(dead_code)]
pub static BORDER_NAME_DARK_STYLE: Lazy<Style> = Lazy::new(|| Style {
fg: color!(Color::White),
bg: None,
underline_color: None,
add_modifier: Modifier::empty(),
sub_modifier: Modifier::empty(),
});
define_style!(TEXT_DARK_STYLE,
fg: color!(Color::White)
);

#[allow(dead_code)]
pub static BORDER_NAME_LIGHT_STYLE: Lazy<Style> = Lazy::new(|| Style {
fg: color!(Color::Black),
bg: None,
underline_color: None,
add_modifier: Modifier::empty(),
sub_modifier: Modifier::empty(),
});
define_style!(DASHBOARD_SECTION_STYLE,
fg: color!(Color::LightRed)
);

pub static DATETIME_DARK_STYLE: Lazy<Style> = Lazy::new(|| Style {
fg: color!(Color::Rgb(173, 173, 184)),
bg: None,
underline_color: None,
add_modifier: Modifier::empty(),
sub_modifier: Modifier::empty(),
});
define_style!(DATETIME_DARK_STYLE,
fg: color!(Color::Rgb(173, 173, 184))
);

pub static DATETIME_LIGHT_STYLE: Lazy<Style> = Lazy::new(|| Style {
fg: color!(Color::Rgb(83, 83, 95)),
bg: None,
underline_color: None,
add_modifier: Modifier::empty(),
sub_modifier: Modifier::empty(),
});
define_style!(DATETIME_LIGHT_STYLE,
fg: color!(Color::Rgb(83, 83, 95))
);

pub static HIGHLIGHT_NAME_DARK_STYLE: Lazy<Style> = Lazy::new(|| Style {
define_style!(HIGHLIGHT_NAME_DARK_STYLE,
fg: color!(Color::Black),
bg: color!(Color::White),
underline_color: None,
add_modifier: *BOLD,
sub_modifier: Modifier::empty(),
});
add_modifier: *BOLD
);

pub static HIGHLIGHT_NAME_LIGHT_STYLE: Lazy<Style> = Lazy::new(|| Style {
define_style!(HIGHLIGHT_NAME_LIGHT_STYLE,
fg: color!(Color::White),
bg: color!(Color::Black),
underline_color: None,
add_modifier: *BOLD,
sub_modifier: Modifier::empty(),
});
add_modifier: *BOLD
);

pub static COLUMN_TITLE_STYLE: Lazy<Style> = Lazy::new(|| Style {
define_style!(COLUMN_TITLE_STYLE,
fg: color!(Color::LightCyan),
bg: None,
underline_color: None,
add_modifier: *BOLD,
sub_modifier: Modifier::empty(),
});
add_modifier: *BOLD
);

pub static SYSTEM_CHAT_STYLE: Lazy<Style> = Lazy::new(|| Style {
define_style!(SYSTEM_CHAT_STYLE,
fg: color!(Color::Red),
bg: None,
underline_color: None,
add_modifier: *BOLD,
sub_modifier: Modifier::empty(),
});
add_modifier: *BOLD
);

pub static DASHBOARD_TITLE_COLOR_STYLE: Lazy<Style> = Lazy::new(|| Style {
fg: color!(Color::Rgb(135, 120, 165)),
bg: None,
underline_color: None,
add_modifier: Modifier::empty(),
sub_modifier: Modifier::empty(),
});
define_style!(DASHBOARD_TITLE_COLOR_STYLE,
fg: color!(Color::Rgb(135, 120, 165))
);

pub static SEARCH_STYLE: Lazy<Style> = Lazy::new(|| Style {
define_style!(SEARCH_STYLE,
fg: color!(Color::Red),
bg: None,
underline_color: None,
add_modifier: *BOLD,
sub_modifier: Modifier::empty(),
});
add_modifier: *BOLD
);

pub static TITLE_STYLE: Lazy<Style> = Lazy::new(|| Style {
define_style!(TITLE_STYLE,
fg: color!(Color::Red),
bg: None,
underline_color: None,
add_modifier: *BOLD,
sub_modifier: Modifier::empty(),
});
add_modifier: *BOLD
);

0 comments on commit 92dc384

Please sign in to comment.