diff --git a/src/main.rs b/src/main.rs
index f70a22d..9decfbd 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -3,8 +3,6 @@ use crate::x::*;
use std::sync::Arc;
-use x11::xrender::XRenderColor;
-use x11::xft::XftColor;
use xcb::{ Xid };
use xcb::x::{
ClientMessageData,
@@ -24,9 +22,8 @@ mod x;
struct DockConfiguration {
thickness: u16,
side: Side,
- background_pixel: u32,
- foreground_pixel: u32,
- foreground_render_color: XRenderColor,
+ background: Color,
+ foreground: Color,
use_rounded_inside_corners: bool,
use_rounded_outside_corners: bool,
}
@@ -47,19 +44,15 @@ fn main() -> xcb::Result<()> {
let display = Arc::new(Display::new()?);
// #d4ffcc
- let background_pixel = display.allocate_color(54484, 65535, 52428)?;
+ let background = display.allocate_color(54484, 65535, 52428)?;
// #5c01be
- let foreground_pixel = display.allocate_color(23644, 257, 48830)?;
- let foreground_render_color = XRenderColor {
- red: 23644, green: 257, blue: 48830, alpha: 65535,
- };
+ let foreground = display.allocate_color(23644, 257, 48830)?;
let dock = Dock::new(Arc::clone(&display), DockConfiguration {
thickness: 34,
side: Side::Bottom,
- background_pixel,
- foreground_pixel, foreground_render_color,
+ background, foreground,
use_rounded_inside_corners: false,
use_rounded_outside_corners: true,
})?;
@@ -110,7 +103,7 @@ impl Dock {
class: WindowClass::InputOutput,
visual: screen.root_visual(),
value_list: &[
- Cw::BackPixel(configuration.background_pixel),
+ Cw::BackPixel(configuration.background.as_pixel()),
Cw::EventMask(EventMask::EXPOSURE | EventMask::BUTTON_PRESS),
],
})?;
@@ -275,7 +268,7 @@ impl Dock {
let cookie = self.send_request_checked(&ChangeGc {
gc: context.graphic_context_id(),
value_list: &[
- Gc::Foreground(self.configuration.background_pixel),
+ Gc::Foreground(self.configuration.background.as_pixel()),
],
});
self.check_request(cookie)?;
@@ -312,13 +305,10 @@ impl Dock {
fn draw_text(&self) -> xcb::Result<()> {
let context = FontGraphicContext::new(self.window())?;
-
- let xft_color = XftColor {
- pixel: self.configuration.foreground_pixel as u64,
- color: self.configuration.foreground_render_color,
- };
+ let color = &self.configuration.foreground;
let text = "playful cool fic tech nonfic";
- self.font.draw(&context, &xft_color, 12, 25, text)?;
+
+ self.font.draw(&context, color, 12, 25, text)?;
Ok(())
}
diff --git a/src/x.rs b/src/x.rs
index 8fd7b0c..60a24bc 100644
--- a/src/x.rs
+++ b/src/x.rs
@@ -1,4 +1,4 @@
-pub use color::DisplayColorExt;
+pub use color::{ Color, DisplayColorExt };
pub use context::{ GraphicContext, HasGraphicContext, GraphicContextExt };
pub use connection::{ Connection, HasConnection, ConnectionExt };
pub use display::{ Display, HasDisplay, DisplayExt };
diff --git a/src/x/color.rs b/src/x/color.rs
index 10ad651..d770379 100644
--- a/src/x/color.rs
+++ b/src/x/color.rs
@@ -7,9 +7,48 @@
use crate::x::connection::*;
use crate::x::display::*;
+use x11::xrender::XRenderColor;
+use x11::xft::XftColor;
use xcb::x::{ AllocColor, Visualtype };
+pub struct Color {
+ /* We need to store both the original 16-bit components and the pixel
+ * value. Additionally, there are situations in which we need to return
+ * an XftColor, which contains both of those. So, we might as well use it
+ * as the authoritative format.
+ *
+ * We treat the contents as opaque, to allow us to pretend it's
+ * something cleaner. :)
+ */
+ font_color: XftColor,
+}
+
+
+impl Color {
+ /* Please notice that the constructor is private. */
+ fn new(pixel: u32, red: u16, green: u16, blue: u16) -> Self {
+ Color {
+ font_color: XftColor {
+ pixel: pixel as u64,
+ color: XRenderColor {
+ red, green, blue,
+ alpha: 65535,
+ }
+ }
+ }
+ }
+
+ pub fn as_pixel(&self) -> u32 {
+ self.font_color.pixel as u32
+ }
+
+ pub fn as_xft_color(&self) -> &XftColor {
+ &self.font_color
+ }
+}
+
+
pub trait DisplayColorExt: DisplayExt {
/* This is sufficiently obscure knowledge that it's worth keeping here
* even though it's unused.
@@ -30,7 +69,7 @@ pub trait DisplayColorExt: DisplayExt {
}
fn allocate_color(&self, red: u16, green: u16, blue: u16)
- -> xcb::Result<u32>
+ -> xcb::Result<Color>
{
let connection = self.connection();
@@ -41,7 +80,9 @@ pub trait DisplayColorExt: DisplayExt {
red, green, blue,
});
- Ok(connection.wait_for_reply(cookie)?.pixel())
+ let pixel = connection.wait_for_reply(cookie)?.pixel();
+
+ Ok(Color::new(pixel, red, green, blue))
}
}
diff --git a/src/x/font.rs b/src/x/font.rs
index d12cbe8..e0e3602 100644
--- a/src/x/font.rs
+++ b/src/x/font.rs
@@ -3,16 +3,11 @@ use crate::x::*;
use std::ffi::CString;
use std::sync::Arc;
-use x11::xft::{
- XftColor,
- XftDraw,
- XftDrawStringUtf8,
- XftFontOpenName,
-};
+use x11::xft::{ XftDrawStringUtf8, XftFontOpenName };
pub struct Font {
- display: Arc<Display>,
+ _display: Arc<Display>,
font_state: *mut x11::xft::XftFont,
}
@@ -27,17 +22,17 @@ impl Font {
XftFontOpenName(xlib_display, screen_index, name.as_ptr().cast())
};
- Ok(Font { display, font_state })
+ Ok(Font { _display: display, font_state })
}
- pub fn draw(&self, context: &FontGraphicContext, xft_color: &XftColor,
+ pub fn draw(&self, context: &FontGraphicContext, color: &Color,
x: i32, y: i32, text: &str)
-> xcb::Result<()>
{
let xft_draw = context.get_xft_draw();
let text = CString::new(text).unwrap();
unsafe {
- XftDrawStringUtf8(xft_draw, xft_color, self.font_state, x, y,
+ XftDrawStringUtf8(xft_draw, color.as_xft_color(), self.font_state, x, y,
text.as_ptr().cast(), text.count_bytes() as i32)
};
diff --git a/src/x/font_context.rs b/src/x/font_context.rs
index 30e5165..56eb08e 100644
--- a/src/x/font_context.rs
+++ b/src/x/font_context.rs
@@ -7,7 +7,7 @@ use xcb::{ Xid };
pub struct FontGraphicContext {
- display: Arc<Display>,
+ _display: Arc<Display>,
context_state: *mut XftDraw,
}
@@ -30,7 +30,7 @@ impl FontGraphicContext {
colormap.resource_id() as u64)
};
- Ok(FontGraphicContext { display, context_state })
+ Ok(FontGraphicContext { _display: display, context_state })
}
pub fn get_xft_draw(&self) -> *mut XftDraw {
|