Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ windows-sys = { version = "0.60", features = [
] }
windows = { version = "0.61", features = [
"Foundation_Numerics",
"UI_ViewManagement",
"Win32_Foundation",
"Win32_Graphics_Direct2D",
"Win32_Graphics_Direct2D_Common",
Expand Down
7 changes: 5 additions & 2 deletions examples/widgets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use winio::{
LinearGradientBrush, Margin, MessageBox, MessageBoxButton, ObservableVec, ObservableVecEvent,
Orient, Point, Progress, RadialGradientBrush, RadioButton, RadioButtonGroup, Rect,
RelativePoint, RelativeSize, Size, SolidColorBrush, StackPanel, TextBox, VAlign, Visible,
Window, WindowEvent, init, start,
Window, WindowEvent, accent_color, init, start,
};

fn main() {
Expand Down Expand Up @@ -382,7 +382,10 @@ impl Component for MainModel {
let brush3 = RadialGradientBrush::new(
[
GradientStop::new(Color::new(0xF5, 0xF5, 0xF5, 0xFF), 0.0),
GradientStop::new(Color::new(0xFF, 0xC0, 0xCB, 0xFF), 1.0),
GradientStop::new(
accent_color().unwrap_or(Color::new(0xFF, 0xC0, 0xCB, 0xFF)),
1.0,
),
],
RelativePoint::new(0.5, 0.5),
RelativePoint::new(0.2, 0.5),
Expand Down
6 changes: 6 additions & 0 deletions src/ui/gtk/accent.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use crate::Color;

/// Get the accent color.
pub fn accent_color() -> Option<Color> {
None
}
3 changes: 3 additions & 0 deletions src/ui/gtk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ pub use combo_box::*;
mod check_box;
pub use check_box::*;

mod accent;
pub use accent::*;

/// GTK [`Window`].
///
/// [`Window`]: gtk4::Window
Expand Down
25 changes: 25 additions & 0 deletions src/ui/mac/accent.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use objc2_app_kit::{NSColor, NSColorSpace};

use crate::Color;

/// Get the accent color.
pub fn accent_color() -> Option<Color> {
unsafe {
let accent = NSColor::controlAccentColor();
let color_space = NSColorSpace::genericRGBColorSpace();
let accent = accent.colorUsingColorSpace(&color_space);
accent.map(|accent| {
let mut r: f64 = 0.0;
let mut g: f64 = 0.0;
let mut b: f64 = 0.0;
let mut a: f64 = 0.0;
accent.getRed_green_blue_alpha(&mut r, &mut g, &mut b, &mut a);
Color::new(
(r * 255.0) as u8,
(g * 255.0) as u8,
(b * 255.0) as u8,
(a * 255.0) as u8,
)
})
}
}
3 changes: 3 additions & 0 deletions src/ui/mac/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ pub use progress::*;
mod combo_box;
pub use combo_box::*;

mod accent;
pub use accent::*;

/// [`NSWindow`].
pub type RawWindow = Retained<NSWindow>;

Expand Down
2 changes: 1 addition & 1 deletion src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub mod export {
filebox::*,
monitor::*,
msgbox::*,
sys::{Brush, Pen, RawWindow},
sys::{Brush, Pen, RawWindow, accent_color},
window_handle::*,
};
}
Expand Down
11 changes: 11 additions & 0 deletions src/ui/qt/canvas.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "canvas.hpp"
#include <QApplication>
#include <QBrush>
#include <QFont>
#include <QLinearGradient>
Expand Down Expand Up @@ -92,6 +93,16 @@ void painter_draw_text(QPainter &p, QRectF rect, rust::Str text) {

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

bool color_accent(QColor &c) {
#if QT_VERSION >= QT_VERSION_CHECK(6, 6, 0)
auto accent = QApplication::palette().color(QPalette::Accent);
new (&c) QColor{accent};
return true;
#else
return false;
#endif
}

std::unique_ptr<QBrush> new_brush(QColor const &c) {
return std::make_unique<QBrush>(c);
}
Expand Down
1 change: 1 addition & 0 deletions src/ui/qt/canvas.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ QSizeF painter_measure_text(QPainter &p, QRectF rect, rust::Str text);
void painter_draw_text(QPainter &p, QRectF rect, rust::Str text);

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

Expand Down
18 changes: 18 additions & 0 deletions src/ui/qt/canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,11 @@ impl DrawingImage {
}
}

/// Get the accent color.
pub fn accent_color() -> Option<Color> {
QColor::accent().map(From::from)
}

#[repr(i32)]
#[non_exhaustive]
#[allow(clippy::enum_variant_names)]
Expand Down Expand Up @@ -564,6 +569,18 @@ impl QColor {
ffi::color_transparent(Pin::new(&mut c));
c
}

pub fn accent() -> Option<Self> {
let mut c = Self {
cspec: Spec::Invalid,
ct: [0; 5],
};
if ffi::color_accent(Pin::new(&mut c)) {
Some(c)
} else {
None
}
}
}

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

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

Expand Down
17 changes: 17 additions & 0 deletions src/ui/windows/accent.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use windows::{
UI::ViewManagement::{UIColorType, UISettings},
core::Result,
};

use crate::Color;

/// Get the accent color.
pub fn accent_color() -> Option<Color> {
accent_impl().ok()
}

fn accent_impl() -> Result<Color> {
let settings = UISettings::new()?;
let accent = settings.GetColorValue(UIColorType::Accent)?;
Ok(Color::new(accent.R, accent.G, accent.B, accent.A))
}
3 changes: 3 additions & 0 deletions src/ui/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ pub use check_box::*;
mod radio_button;
pub use radio_button::*;

mod accent;
pub use accent::*;

use crate::ColorTheme;

pub fn color_theme() -> ColorTheme {
Expand Down