Skip to content

Commit 996c7a1

Browse files
authored
Merge pull request #30 from compio-rs/dev/accent
feat: accent color
2 parents 9d8562f + 9193d6d commit 996c7a1

12 files changed

Lines changed: 94 additions & 3 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ windows-sys = { version = "0.60", features = [
5050
] }
5151
windows = { version = "0.61", features = [
5252
"Foundation_Numerics",
53+
"UI_ViewManagement",
5354
"Win32_Foundation",
5455
"Win32_Graphics_Direct2D",
5556
"Win32_Graphics_Direct2D_Common",

examples/widgets.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use winio::{
55
LinearGradientBrush, Margin, MessageBox, MessageBoxButton, ObservableVec, ObservableVecEvent,
66
Orient, Point, Progress, RadialGradientBrush, RadioButton, RadioButtonGroup, Rect,
77
RelativePoint, RelativeSize, Size, SolidColorBrush, StackPanel, TextBox, VAlign, Visible,
8-
Window, WindowEvent, init, start,
8+
Window, WindowEvent, accent_color, init, start,
99
};
1010

1111
fn main() {
@@ -382,7 +382,10 @@ impl Component for MainModel {
382382
let brush3 = RadialGradientBrush::new(
383383
[
384384
GradientStop::new(Color::new(0xF5, 0xF5, 0xF5, 0xFF), 0.0),
385-
GradientStop::new(Color::new(0xFF, 0xC0, 0xCB, 0xFF), 1.0),
385+
GradientStop::new(
386+
accent_color().unwrap_or(Color::new(0xFF, 0xC0, 0xCB, 0xFF)),
387+
1.0,
388+
),
386389
],
387390
RelativePoint::new(0.5, 0.5),
388391
RelativePoint::new(0.2, 0.5),

src/ui/gtk/accent.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
use crate::Color;
2+
3+
/// Get the accent color.
4+
pub fn accent_color() -> Option<Color> {
5+
None
6+
}

src/ui/gtk/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ pub use combo_box::*;
3737
mod check_box;
3838
pub use check_box::*;
3939

40+
mod accent;
41+
pub use accent::*;
42+
4043
/// GTK [`Window`].
4144
///
4245
/// [`Window`]: gtk4::Window

src/ui/mac/accent.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
use objc2_app_kit::{NSColor, NSColorSpace};
2+
3+
use crate::Color;
4+
5+
/// Get the accent color.
6+
pub fn accent_color() -> Option<Color> {
7+
unsafe {
8+
let accent = NSColor::controlAccentColor();
9+
let color_space = NSColorSpace::genericRGBColorSpace();
10+
let accent = accent.colorUsingColorSpace(&color_space);
11+
accent.map(|accent| {
12+
let mut r: f64 = 0.0;
13+
let mut g: f64 = 0.0;
14+
let mut b: f64 = 0.0;
15+
let mut a: f64 = 0.0;
16+
accent.getRed_green_blue_alpha(&mut r, &mut g, &mut b, &mut a);
17+
Color::new(
18+
(r * 255.0) as u8,
19+
(g * 255.0) as u8,
20+
(b * 255.0) as u8,
21+
(a * 255.0) as u8,
22+
)
23+
})
24+
}
25+
}

src/ui/mac/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ pub use progress::*;
3131
mod combo_box;
3232
pub use combo_box::*;
3333

34+
mod accent;
35+
pub use accent::*;
36+
3437
/// [`NSWindow`].
3538
pub type RawWindow = Retained<NSWindow>;
3639

src/ui/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub mod export {
3737
filebox::*,
3838
monitor::*,
3939
msgbox::*,
40-
sys::{Brush, Pen, RawWindow},
40+
sys::{Brush, Pen, RawWindow, accent_color},
4141
window_handle::*,
4242
};
4343
}

src/ui/qt/canvas.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "canvas.hpp"
2+
#include <QApplication>
23
#include <QBrush>
34
#include <QFont>
45
#include <QLinearGradient>
@@ -92,6 +93,16 @@ void painter_draw_text(QPainter &p, QRectF rect, rust::Str text) {
9293

9394
void color_transparent(QColor &c) { new (&c) QColor{Qt::transparent}; }
9495

96+
bool color_accent(QColor &c) {
97+
#if QT_VERSION >= QT_VERSION_CHECK(6, 6, 0)
98+
auto accent = QApplication::palette().color(QPalette::Accent);
99+
new (&c) QColor{accent};
100+
return true;
101+
#else
102+
return false;
103+
#endif
104+
}
105+
95106
std::unique_ptr<QBrush> new_brush(QColor const &c) {
96107
return std::make_unique<QBrush>(c);
97108
}

src/ui/qt/canvas.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ QSizeF painter_measure_text(QPainter &p, QRectF rect, rust::Str text);
5151
void painter_draw_text(QPainter &p, QRectF rect, rust::Str text);
5252

5353
void color_transparent(QColor &c);
54+
bool color_accent(QColor &c);
5455
std::unique_ptr<QBrush> new_brush(QColor const &c);
5556
std::unique_ptr<QPen> new_pen(QBrush const &b, double width);
5657

src/ui/qt/canvas.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,11 @@ impl DrawingImage {
468468
}
469469
}
470470

471+
/// Get the accent color.
472+
pub fn accent_color() -> Option<Color> {
473+
QColor::accent().map(From::from)
474+
}
475+
471476
#[repr(i32)]
472477
#[non_exhaustive]
473478
#[allow(clippy::enum_variant_names)]
@@ -564,6 +569,18 @@ impl QColor {
564569
ffi::color_transparent(Pin::new(&mut c));
565570
c
566571
}
572+
573+
pub fn accent() -> Option<Self> {
574+
let mut c = Self {
575+
cspec: Spec::Invalid,
576+
ct: [0; 5],
577+
};
578+
if ffi::color_accent(Pin::new(&mut c)) {
579+
Some(c)
580+
} else {
581+
None
582+
}
583+
}
567584
}
568585

569586
impl From<Color> for QColor {
@@ -695,6 +712,7 @@ mod ffi {
695712
fn setPen(self: Pin<&mut QPainter>, color: &QColor);
696713

697714
fn color_transparent(c: Pin<&mut QColor>);
715+
fn color_accent(c: Pin<&mut QColor>) -> bool;
698716
fn new_brush(c: &QColor) -> UniquePtr<QBrush>;
699717
fn new_pen(b: &QBrush, width: f64) -> UniquePtr<QPen>;
700718

0 commit comments

Comments
 (0)