Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add uiColorButton #73

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
22 changes: 20 additions & 2 deletions iui/examples/basic.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
extern crate iui;
use iui::controls::{Button, ColorButton, Group, Label, Rgba, VerticalBox};
use iui::prelude::*;
use iui::controls::{Label, Button, VerticalBox, Group};

fn main() {
// Initialize the UI library
let ui = UI::init().expect("Couldn't initialize UI library");
// Create a window into which controls can be placed
let mut win = Window::new(&ui, "Test App", 200, 200, WindowType::NoMenubar);

// Create a vertical layout to hold the controls
let mut vbox = VerticalBox::new(&ui);
vbox.set_padded(&ui, true);
Expand All @@ -32,6 +32,23 @@ fn main() {
}
});

let mut color_button = ColorButton::new(&ui);
color_button.set_color(
&ui,
Rgba {
r: 1.0,
g: 0.0,
b: 0.0,
a: 1.0,
},
);
color_button.on_changed(&ui, {
let ui = ui.clone();
move |btn| {
dbg!(btn.color(&ui));
}
});

// Create a new label. Note that labels don't auto-wrap!
let mut label_text = String::new();
label_text.push_str("There is a ton of text in this label.\n");
Expand All @@ -42,6 +59,7 @@ fn main() {
vbox.append(&ui, label, LayoutStrategy::Stretchy);
group_vbox.append(&ui, button, LayoutStrategy::Compact);
group_vbox.append(&ui, quit_button, LayoutStrategy::Compact);
group_vbox.append(&ui, color_button, LayoutStrategy::Compact);
group.set_child(&ui, group_vbox);
vbox.append(&ui, group, LayoutStrategy::Compact);

Expand Down
68 changes: 67 additions & 1 deletion iui/src/controls/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@ use std::os::raw::c_void;
use std::ffi::{CStr, CString};
use std::mem;
use ui::UI;
use ui_sys::{self, uiButton, uiControl, uiLabel};
use ui_sys::{self, uiButton, uiColorButton, uiControl, uiLabel};

#[derive(Debug, Clone)]
pub struct Rgba {
pub r: f64,
pub g: f64,
pub b: f64,
pub a: f64,
}

define_control!{
/// A non-interactable piece of text.
Expand All @@ -17,6 +25,12 @@ define_control!{
sys_type: uiButton
}

define_control!{
/// A color chooser button which users can click on, causing a callback to run.
rust_type: ColorButton,
sys_type: uiColorButton
}

impl Button {
/// Create a new button with the given text as its label.
pub fn new(_ctx: &UI, text: &str) -> Button {
Expand Down Expand Up @@ -69,6 +83,58 @@ impl Button {
}
}

impl ColorButton {
/// Create a new color button
pub fn new(_ctx: &UI) -> ColorButton {
unsafe { ColorButton::from_raw(ui_sys::uiNewColorButton()) }
}

pub fn color(&self, _ctx: &UI) -> Rgba {
let mut r: f64 = 0.0;
let mut g: f64 = 0.0;
let mut b: f64 = 0.0;
let mut a: f64 = 0.0;

unsafe {
ui_sys::uiColorButtonColor(self.uiColorButton, &mut r, &mut g, &mut b, &mut a);
Rgba { r, g, b, a }
}
}

/// Set the text on the button.
pub fn set_color(&mut self, _ctx: &UI, color: Rgba) {
unsafe {
ui_sys::uiColorButtonSetColor(self.uiColorButton, color.r, color.g, color.b, color.a)
}
}

/// Run the given callback when the color button is clicked.
pub fn on_changed<'ctx, F: FnMut(&mut ColorButton) + 'ctx>(
&mut self,
_ctx: &'ctx UI,
callback: F,
) {
unsafe {
let mut data: Box<Box<FnMut(&mut ColorButton)>> = Box::new(Box::new(callback));
ui_sys::uiColorButtonOnChanged(
self.uiColorButton,
Some(c_callback),
&mut *data as *mut Box<FnMut(&mut ColorButton)> as *mut c_void,
);
mem::forget(data);
}

extern "C" fn c_callback(button: *mut uiColorButton, data: *mut c_void) {
unsafe {
let mut button = ColorButton {
uiColorButton: button,
};
mem::transmute::<*mut c_void, &mut Box<FnMut(&mut ColorButton)>>(data)(&mut button)
}
}
}
}

impl Label {
/// Create a new label with the given string as its text.
/// Note that labels do not auto-wrap their text; they will expand as far as needed
Expand Down