Skip to content

Commit

Permalink
#272 simplifying TextStyle and making it Copy
Browse files Browse the repository at this point in the history
  • Loading branch information
sminez committed Jul 15, 2023
1 parent cf477f7 commit 070e1c2
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 47 deletions.
4 changes: 2 additions & 2 deletions crates/penrose_ui/src/bar/widgets/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub struct ActiveWindowId {

impl ActiveWindowId {
/// Create a new ActiveWindowId widget.
pub fn new(style: &TextStyle, is_greedy: bool, right_justified: bool) -> Self {
pub fn new(style: TextStyle, is_greedy: bool, right_justified: bool) -> Self {
Self {
inner: Text::new("", style, is_greedy, right_justified),
}
Expand Down Expand Up @@ -60,7 +60,7 @@ pub struct StateSummary {

impl StateSummary {
/// Create a new StateSummary widget.
pub fn new(style: &TextStyle) -> Self {
pub fn new(style: TextStyle) -> Self {
Self {
inner: Text::new("", style, false, false),
cfg: CurrentStateConfig {
Expand Down
18 changes: 5 additions & 13 deletions crates/penrose_ui/src/bar/widgets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@ where
#[derive(Clone, Debug, PartialEq)]
pub struct Text {
txt: String,
font: String,
point_size: u8,
fg: Color,
bg: Option<Color>,
padding: (u32, u32),
Expand All @@ -96,14 +94,12 @@ impl Text {
/// Construct a new [Text]
pub fn new(
txt: impl Into<String>,
style: &TextStyle,
style: TextStyle,
is_greedy: bool,
right_justified: bool,
) -> Self {
Self {
txt: txt.into(),
font: style.font.clone(),
point_size: style.point_size,
fg: style.fg,
bg: style.bg,
padding: style.padding,
Expand Down Expand Up @@ -213,11 +209,9 @@ impl<X: XConn> Widget<X> for Text {
/// }
///
/// let style = TextStyle {
/// font: "mono".to_string(),
/// point_size: 10,
/// fg: 0xebdbb2ff.into(),
/// bg: Some(0x282828ff.into()),
/// padding: (2.0, 2.0),
/// padding: (2, 2),
/// };
///
/// let my_widget = RefreshText::new(&style, my_get_text);
Expand All @@ -238,7 +232,7 @@ impl fmt::Debug for RefreshText {
impl RefreshText {
/// Construct a new [`RefreshText`] using the specified styling and a function for
/// generating the widget contents.
pub fn new<F>(style: &TextStyle, get_text: F) -> Self
pub fn new<F>(style: TextStyle, get_text: F) -> Self
where
F: Fn() -> String + 'static,
{
Expand Down Expand Up @@ -302,11 +296,9 @@ impl<X: XConn> Widget<X> for RefreshText {
/// }
///
/// let style = TextStyle {
/// font: "mono".to_string(),
/// point_size: 10,
/// fg: 0xebdbb2ff.into(),
/// bg: Some(0x282828ff.into()),
/// padding: (2.0, 2.0),
/// padding: (2, 2),
/// };
///
///
Expand All @@ -325,7 +317,7 @@ impl IntervalText {
/// Construct a new [`IntervalText`] using the specified styling and a function for
/// generating the widget contents. The function for updating the widget contents
/// will be run in its own thread on the interval provided.
pub fn new<F>(style: &TextStyle, get_text: F, interval: Duration) -> Self
pub fn new<F>(style: TextStyle, get_text: F, interval: Duration) -> Self
where
F: Fn() -> String + 'static + Send,
{
Expand Down
11 changes: 3 additions & 8 deletions crates/penrose_ui/src/bar/widgets/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct RootWindowName {

impl RootWindowName {
/// Create a new RootWindowName widget
pub fn new(style: &TextStyle, is_greedy: bool, right_justified: bool) -> Self {
pub fn new(style: TextStyle, is_greedy: bool, right_justified: bool) -> Self {
Self {
inner: Text::new("penrose", style, is_greedy, right_justified),
}
Expand Down Expand Up @@ -69,12 +69,7 @@ impl ActiveWindowName {
/// Create a new ActiveWindowName widget with a maximum character count.
///
/// max_chars can not be lower than 3.
pub fn new(
max_chars: usize,
style: &TextStyle,
is_greedy: bool,
right_justified: bool,
) -> Self {
pub fn new(max_chars: usize, style: TextStyle, is_greedy: bool, right_justified: bool) -> Self {
Self {
inner: Text::new("", style, is_greedy, right_justified),
max_chars: max_chars.max(3),
Expand Down Expand Up @@ -149,7 +144,7 @@ pub struct CurrentLayout {

impl CurrentLayout {
/// Create a new CurrentLayout widget
pub fn new(style: &TextStyle) -> Self {
pub fn new(style: TextStyle) -> Self {
Self {
inner: Text::new("", style, false, false),
}
Expand Down
8 changes: 4 additions & 4 deletions crates/penrose_ui/src/bar/widgets/sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::fs;
///
/// If the given battery name is not found on this system, this widget will
/// render as an empty string.
pub fn battery_summary(bat: &'static str, style: &TextStyle) -> RefreshText {
pub fn battery_summary(bat: &'static str, style: TextStyle) -> RefreshText {
RefreshText::new(style, move || battery_text(bat).unwrap_or_default())
}

Expand Down Expand Up @@ -44,7 +44,7 @@ fn read_sys_file(bat: &str, fname: &str) -> Option<String> {
/// Display the current date and time in YYYY-MM-DD HH:MM format
///
/// This widget shells out to the `date` tool to generate its output
pub fn current_date_and_time(style: &TextStyle) -> RefreshText {
pub fn current_date_and_time(style: TextStyle) -> RefreshText {
RefreshText::new(style, || {
spawn_for_output_with_args("date", &["+%F %R"])
.unwrap_or_default()
Expand All @@ -55,7 +55,7 @@ pub fn current_date_and_time(style: &TextStyle) -> RefreshText {

/// Display the ESSID currently connected to and the signal quality as
/// a percentage.
pub fn wifi_network(style: &TextStyle) -> RefreshText {
pub fn wifi_network(style: TextStyle) -> RefreshText {
RefreshText::new(style, move || wifi_text().unwrap_or_default())
}

Expand Down Expand Up @@ -98,7 +98,7 @@ fn signal_quality(interface: &str) -> Option<String> {
}

/// Display the current volume level as reported by `amixer`
pub fn amixer_volume(channel: &'static str, style: &TextStyle) -> RefreshText {
pub fn amixer_volume(channel: &'static str, style: TextStyle) -> RefreshText {
RefreshText::new(style, move || amixer_text(channel).unwrap_or_default())
}

Expand Down
6 changes: 1 addition & 5 deletions crates/penrose_ui/src/bar/widgets/workspaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ fn focused_workspaces<X: XConn>(state: &State<X>) -> Vec<String> {
pub struct Workspaces {
workspaces: Vec<WsMeta>,
focused_ws: Vec<String>, // focused ws per screen
font: String,
point_size: u8,
extent: Option<(u32, u32)>,
fg_1: Color,
fg_2: Color,
Expand All @@ -69,12 +67,10 @@ pub struct Workspaces {

impl Workspaces {
/// Construct a new WorkspaceWidget
pub fn new(style: &TextStyle, highlight: impl Into<Color>, empty_fg: impl Into<Color>) -> Self {
pub fn new(style: TextStyle, highlight: impl Into<Color>, empty_fg: impl Into<Color>) -> Self {
Self {
workspaces: vec![],
focused_ws: vec![], // set in startup hook
font: style.font.clone(),
point_size: style.point_size,
extent: None,
fg_1: style.fg,
fg_2: empty_fg.into(),
Expand Down
6 changes: 1 addition & 5 deletions crates/penrose_ui/src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,9 @@ use fontset::Fontset;

pub(crate) const SCREEN: i32 = 0;

#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
/// A set of styling options for a text string
pub struct TextStyle {
/// Font name to use for rendering
pub font: String,
/// Point size to render the font at
pub point_size: u8,
/// Foreground color in 0xRRGGBB format
pub fg: Color,
/// Optional background color in 0xRRGGBB format (default to current background if None)
Expand Down
16 changes: 9 additions & 7 deletions crates/penrose_ui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ pub type Result<T> = std::result::Result<T, Error>;
/// WM_NAME property of the root window.
pub fn status_bar<X: XConn>(
height: u32,
style: &TextStyle,
font: &str,
point_size: u8,
style: TextStyle,
highlight: impl Into<Color>,
empty_ws: impl Into<Color>,
position: Position,
Expand All @@ -108,25 +110,25 @@ pub fn status_bar<X: XConn>(
position,
height,
style.bg.unwrap_or_else(|| 0x000000.into()),
&style.font,
style.point_size,
font,
point_size,
vec![
Box::new(Workspaces::new(style, highlight, empty_ws)),
Box::new(CurrentLayout::new(style)),
Box::new(ActiveWindowName::new(
max_active_window_chars,
&TextStyle {
TextStyle {
bg: Some(highlight),
padding: (6, 4),
..style.clone()
..style
},
true,
false,
)),
Box::new(RootWindowName::new(
&TextStyle {
TextStyle {
padding: (4, 2),
..style.clone()
..style
},
false,
true,
Expand Down
4 changes: 1 addition & 3 deletions examples/status_bar/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,12 @@ fn main() -> Result<()> {
let conn = RustConn::new()?;
let key_bindings = parse_keybindings_with_xmodmap(raw_key_bindings())?;
let style = TextStyle {
font: FONT.to_string(),
point_size: 8,
fg: WHITE.into(),
bg: Some(BLACK.into()),
padding: (2, 2),
};

let bar = status_bar(BAR_HEIGHT_PX, &style, BLUE, GREY, Position::Top).unwrap();
let bar = status_bar(BAR_HEIGHT_PX, FONT, 8, style, BLUE, GREY, Position::Top).unwrap();

let wm = bar.add_to(WindowManager::new(
config,
Expand Down

0 comments on commit 070e1c2

Please sign in to comment.