Skip to content

Commit

Permalink
Add FileSelect Widget (#47)
Browse files Browse the repository at this point in the history
* Start on FileSelect

* Convert WASI paths

* obs_property_set_visible

* Remove redundant set=get

* Remove Some(_) case, merge with None case
  • Loading branch information
AlexKnauth authored Mar 14, 2024
1 parent b9c8489 commit d92dd75
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 11 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

111 changes: 104 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ use {
},
livesplit_core::auto_splitting,
livesplit_core::auto_splitting::settings::{Value, Widget, WidgetKind},
livesplit_core::auto_splitting::wasi_path,
log::error,
std::{ffi::CString, sync::atomic::AtomicBool},
};
Expand Down Expand Up @@ -831,11 +832,15 @@ unsafe extern "C" fn settings_list_modified(

Check failure on line 832 in src/lib.rs

View workflow job for this annotation

GitHub Actions / format

Diff in /home/runner/work/obs-livesplit-one/obs-livesplit-one/src/lib.rs
let tooltip_property = obs_properties_get(props, SETTINGS_AUTO_SPLITTER_SETTINGS_TOOLTIP);
let enable_property = obs_properties_get(props, SETTINGS_AUTO_SPLITTER_SETTINGS_ENABLE);
let file_select_property = obs_properties_get(props, SETTINGS_AUTO_SPLITTER_SETTINGS_FILE_SELECT);

macro_rules! reset_auto_splitter_setting_ui {
() => {
obs_property_set_description(tooltip_property, DEFAULT_AUTO_SPLITTER_SETTING_TOOLTIP);
obs_property_set_enabled(enable_property, false);
obs_property_set_enabled(file_select_property, false);
obs_property_set_visible(enable_property, false);
obs_property_set_visible(file_select_property, false);
return true;
};
}
Expand All @@ -857,6 +862,9 @@ unsafe extern "C" fn settings_list_modified(
match user_setting.kind {
WidgetKind::Title { heading_level: _ } => {
obs_property_set_enabled(enable_property, false);
obs_property_set_enabled(file_select_property, false);
obs_property_set_visible(enable_property, false);
obs_property_set_visible(file_select_property, false);
}
WidgetKind::Bool {
default_value: default,
Expand All @@ -870,13 +878,16 @@ unsafe extern "C" fn settings_list_modified(
{
Some(Value::Bool(value)) => {
obs_property_set_enabled(enable_property, true);
obs_property_set_enabled(file_select_property, false);
obs_property_set_visible(enable_property, true);
obs_property_set_visible(file_select_property, false);
obs_data_set_bool(settings, SETTINGS_AUTO_SPLITTER_SETTINGS_ENABLE, *value);
}
Some(_) => {
warn!("Unknown / unimplemented value type");
}
None => {
_ => {
obs_property_set_enabled(enable_property, true);
obs_property_set_enabled(file_select_property, false);
obs_property_set_visible(enable_property, true);
obs_property_set_visible(file_select_property, false);
obs_data_set_bool(
settings,
SETTINGS_AUTO_SPLITTER_SETTINGS_ENABLE,
Expand All @@ -889,7 +900,35 @@ unsafe extern "C" fn settings_list_modified(
warn!("Unimplemented setting type Choice");
}
WidgetKind::FileSelect { .. } => {
warn!("Unimplemented setting type FileSelect");
match state
.global_timer
.auto_splitter
.settings_map()
.unwrap_or_default()
.get(user_setting.key.as_ref())
{
Some(Value::String(value)) => {
obs_property_set_enabled(enable_property, false);
obs_property_set_enabled(file_select_property, true);

Check failure on line 912 in src/lib.rs

View workflow job for this annotation

GitHub Actions / format

Diff in /home/runner/work/obs-livesplit-one/obs-livesplit-one/src/lib.rs
obs_property_set_visible(enable_property, false);
obs_property_set_visible(file_select_property, true);
let path_cs = wasi_path::to_native(value).filter(|p| p.exists()).and_then(|p| {
CString::new(p.as_os_str().as_encoded_bytes()).ok()
}).unwrap_or_default();
obs_data_set_string(settings, SETTINGS_AUTO_SPLITTER_SETTINGS_FILE_SELECT, path_cs.as_ptr());
}
_ => {
obs_property_set_enabled(enable_property, false);
obs_property_set_enabled(file_select_property, true);
obs_property_set_visible(enable_property, false);
obs_property_set_visible(file_select_property, true);
obs_data_set_string(
settings,
SETTINGS_AUTO_SPLITTER_SETTINGS_FILE_SELECT,
ptr::null(),
);
}
}
}
}

Expand Down Expand Up @@ -930,8 +969,6 @@ unsafe extern "C" fn settings_enable_modified(

let value = obs_data_get_bool(settings, SETTINGS_AUTO_SPLITTER_SETTINGS_ENABLE);

obs_data_set_bool(settings, SETTINGS_AUTO_SPLITTER_SETTINGS_ENABLE, value);

let setting_key = match list_setting_string.to_str() {
Ok(value) => value,
Err(_) => {
Expand All @@ -954,6 +991,53 @@ unsafe extern "C" fn settings_enable_modified(
true
}

#[cfg(feature = "auto-splitting")]
unsafe extern "C" fn settings_file_select_modified(
data: *mut c_void,
_props: *mut obs_properties_t,
_prop: *mut obs_property_t,
settings: *mut obs_data_t,
) -> bool {
let default_setting_string = CStr::from_ptr(DEFAULT_AUTO_SPLITTER_LIST_SETTING);
let list_setting_string = CStr::from_ptr(obs_data_get_string(
settings,
SETTINGS_AUTO_SPLITTER_SETTINGS_LIST,
));

if list_setting_string == default_setting_string {
return false;
}

let state: &mut State = &mut *data.cast();

let value = obs_data_get_string(settings, SETTINGS_AUTO_SPLITTER_SETTINGS_FILE_SELECT);

let setting_key = match list_setting_string.to_str() {
Ok(value) => value,
Err(_) => {
warn!("Tried to set invalid setting key");
return false;
}
};

match state.global_timer.auto_splitter.settings_map() {
Some(mut map) => {
let value_str = CStr::from_ptr(value).to_string_lossy();
let Some(wasi_str) = wasi_path::from_native(Path::new(value_str.as_ref())) else {
warn!("Tried to set invalid setting value");
return false;
};
map.insert(Arc::from(setting_key), Value::String(Arc::from(wasi_str)));
state.global_timer.auto_splitter.set_settings_map(map);
}
None => {
warn!("The settings map could not be loaded");
return false;
}
};
true
}

#[cfg(feature = "auto-splitting")]
unsafe extern "C" fn use_local_auto_splitter_modified(
data: *mut c_void,
Expand Down Expand Up @@ -1285,6 +1369,9 @@ const SETTINGS_AUTO_SPLITTER_SETTINGS_TOOLTIP: *const c_char =
#[cfg(feature = "auto-splitting")]
const SETTINGS_AUTO_SPLITTER_SETTINGS_ENABLE: *const c_char =
cstr!("auto_splitter_settings_enable");
#[cfg(feature = "auto-splitting")]
const SETTINGS_AUTO_SPLITTER_SETTINGS_FILE_SELECT: *const c_char =
cstr!("auto_splitter_settings_file_select");
const SETTINGS_LAYOUT_PATH: *const c_char = cstr!("layout_path");
const SETTINGS_SAVE_SPLITS: *const c_char = cstr!("save_splits");

Expand Down Expand Up @@ -1479,6 +1566,15 @@ unsafe extern "C" fn get_properties(data: *mut c_void) -> *mut obs_properties_t
cstr!("Enable"),
);

let settings_file_select = obs_properties_add_path(
props,
SETTINGS_AUTO_SPLITTER_SETTINGS_FILE_SELECT,
cstr!("Select a file"),
OBS_PATH_FILE,
cstr!("All files (*.*)"),
ptr::null(),
);

obs_property_list_add_string(
settings_list,
cstr!("Select a setting to change"),
Expand All @@ -1498,6 +1594,7 @@ unsafe extern "C" fn get_properties(data: *mut c_void) -> *mut obs_properties_t

Check failure on line 1594 in src/lib.rs

View workflow job for this annotation

GitHub Actions / format

Diff in /home/runner/work/obs-livesplit-one/obs-livesplit-one/src/lib.rs
obs_property_set_modified_callback2(settings_list, Some(settings_list_modified), data);
obs_property_set_modified_callback2(settings_enable, Some(settings_enable_modified), data);
obs_property_set_modified_callback2(settings_file_select, Some(settings_file_select_modified), data);

let auto_splitter_settings = state.global_timer.auto_splitter.settings_widgets();

Expand Down

0 comments on commit d92dd75

Please sign in to comment.