Skip to content

Commit

Permalink
Added ThemeColor and Reaper::get- set_theme_color
Browse files Browse the repository at this point in the history
  • Loading branch information
Levitanus committed Apr 5, 2024
1 parent e35d348 commit 98a374f
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 54 deletions.
1 change: 1 addition & 0 deletions rea-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ serde-pickle = "1.1"
serde_derive = "1.0"
serde_json = "1.0"
chrono = {version="0.4", features=["serde"]}
strum = { version = "0.26", features = ["derive"] }

[dev-dependencies]
rea-rs-macros = {version = "0.1.3", path = "../macros"}
Expand Down
4 changes: 2 additions & 2 deletions rea-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,5 +216,5 @@ pub use envelope::*;
pub mod midi_editor;
pub use midi_editor::*;

#[cfg(test)]
mod test;
pub mod color;
pub use color::*;
49 changes: 0 additions & 49 deletions rea-rs/src/misc_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,55 +12,6 @@ use std::{
time::Duration,
};

#[derive(
Debug, Clone, Copy, PartialEq, PartialOrd, Serialize, Deserialize,
)]
pub struct Color {
pub r: u8,
pub g: u8,
pub b: u8,
}
impl Color {
/// New color from r, g, b (0..255).
pub fn new(r: u8, g: u8, b: u8) -> Self {
Self { r, g, b }
}

/// Get as tuple.
pub fn get(&self) -> (u8, u8, u8) {
(self.r, self.g, self.b)
}

/// Make from the OS-dependent color.
pub fn from_native(native: i32) -> Self {
unsafe {
let low = Reaper::get().low();
let (mut r, mut g, mut b) = (
MaybeUninit::new(0),
MaybeUninit::new(0),
MaybeUninit::new(0),
);
low.ColorFromNative(
native,
r.as_mut_ptr(),
g.as_mut_ptr(),
b.as_mut_ptr(),
);
Self {
r: r.assume_init_read() as u8,
g: g.assume_init_read() as u8,
b: b.assume_init_read() as u8,
}
}
}

/// Convert to OS-dependent color.
pub fn to_native(&self) -> i32 {
let low = Reaper::get().low();
low.ColorToNative(self.r as i32, self.g as i32, self.b as i32)
}
}

#[derive(Debug, PartialEq, PartialOrd, Serialize, Deserialize)]
pub struct Measure {
pub index: u32,
Expand Down
70 changes: 67 additions & 3 deletions rea-rs/src/simple_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ use crate::{
utils::{
as_c_char, as_c_str, as_mut_i8, as_string, make_string_buf, WithNull,
},
AutomationMode, CommandId, MIDIEditor, MessageBoxType, MessageBoxValue,
Project, Reaper, Section, UndoFlags,
AutomationMode, Color, CommandId, MIDIEditor, MessageBoxType,
MessageBoxValue, Project, Reaper, Section, ThemeColor, UndoFlags,
};
use int_enum::IntEnum;
use log::debug;
use std::{
collections::HashMap, error::Error, ffi::CString, fs::canonicalize,
marker::PhantomData, path::Path, ptr::NonNull,
marker::PhantomData, path::Path, ptr::NonNull, str::Utf8Error,
};

impl Reaper {
Expand Down Expand Up @@ -340,6 +340,70 @@ impl Reaper {
}
}

/// get the path for reaper resources: scripts, options etc.
pub fn get_resource_path(&self) -> Result<String, Utf8Error> {
as_string(self.low().GetResourcePath())
}

/// get the color from current Reaper Theme
///
/// * if `from_Theme_file` is `true`, the value returned from a theme file,
/// without possible user modifications.
pub fn get_theme_color(
&self,
category: ThemeColor,
from_theme_file: bool,
) -> Color {
let ini_key = CString::new(category.to_string())
.expect("Can not convert category to CString");
let result = unsafe {
self.low().GetThemeColor(
ini_key.as_ptr(),
match from_theme_file {
true => 1,
false => 0,
},
)
};
assert!(
result != -1,
"Couldn't retrieve theme color of category {category}"
);
Color::from_native(result)
}

/// get the color from current Reaper Theme
///
/// * None is provided ‒ the default theme color will be used.
pub fn set_theme_color(
&mut self,
category: ThemeColor,
color: Option<Color>,
bypass_transforms: bool,
) -> Color {
let ini_key = CString::new(category.to_string())
.expect("Can not convert category to CString");
let result = unsafe {
self.low().SetThemeColor(
ini_key.as_ptr(),
if let Some(c) = color {
c.to_native()
} else {
-1
},
match bypass_transforms {
true => 1,
false => 0,
},
)
};
assert!(
result != -1,
"Couldn't retrieve theme color of category {category}"
);
Color::from_native(result)
}

/// Call function while freezing the UI.
pub fn with_prevent_ui_refresh(&self, f: impl Fn()) {
self.low().PreventUIRefresh(1);
Expand Down

0 comments on commit 98a374f

Please sign in to comment.