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

refactor: shorten widget_id/widgetId -> id #379

Merged
merged 1 commit into from
Feb 1, 2025
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
10 changes: 5 additions & 5 deletions crates/deskulpt-core/src/commands/bundle_widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ use crate::states::StatesExtWidgetCollection;
#[command]
pub async fn bundle_widget<R: Runtime>(
app_handle: AppHandle<R>,
widget_id: String,
id: String,
base_url: String,
apis_blob_url: String,
) -> CmdResult<String> {
let widgets_dir = app_handle.widgets_dir();
let widget_dir = widgets_dir.join(&widget_id);
let widget_dir = widgets_dir.join(&id);

let mut bundler = app_handle.with_widget_collection(|collection| {
collection
.get(&widget_id)
.ok_or_else(|| cmderr!("Widget (id={}) does not exist in the collection", widget_id))?
.get(&id)
.ok_or_else(|| cmderr!("Widget (id={}) does not exist in the collection", id))?
.as_ref()
.map(|config| {
let builder = WidgetBundlerBuilder::new(
Expand All @@ -44,6 +44,6 @@ pub async fn bundle_widget<R: Runtime>(
let code = bundler
.bundle()
.await
.context(format!("Failed to bundle widget (id={})", widget_id))?;
.context(format!("Failed to bundle widget (id={})", id))?;
Ok(code)
}
20 changes: 5 additions & 15 deletions crates/deskulpt-core/src/commands/call_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,30 +29,20 @@ pub async fn call_plugin(
app_handle: AppHandle,
plugin: String,
command: String,
widget_id: String,
id: String,
payload: Option<serde_json::Value>,
) -> CmdResult<serde_json::Value> {
match plugin.as_str() {
"fs" => {
let plugin = FS_PLUGIN.lock().await;
let result = deskulpt_plugin::call_plugin(
app_handle,
&*plugin,
command.as_str(),
widget_id,
payload,
)?;
let result =
deskulpt_plugin::call_plugin(app_handle, &*plugin, command.as_str(), id, payload)?;
Ok(result)
},
"sys" => {
let plugin = SYS_PLUGIN.lock().await;
let result = deskulpt_plugin::call_plugin(
app_handle,
&*plugin,
command.as_str(),
widget_id,
payload,
)?;
let result =
deskulpt_plugin::call_plugin(app_handle, &*plugin, command.as_str(), id, payload)?;
Ok(result)
},
_ => cmdbail!("Unknown plugin: {}", plugin),
Expand Down
6 changes: 3 additions & 3 deletions crates/deskulpt-core/src/commands/rescan_widgets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,21 @@ pub async fn rescan_widgets<R: Runtime>(app_handle: AppHandle<R>) -> CmdResult<W
continue; // Non-directory entries are not widgets, skip
}

let widget_id = match path.file_name() {
let id = match path.file_name() {
Some(file_name) => file_name.to_string_lossy().to_string(),
None => cmdbail!("Invalid widget directory: '{}'", path.display()),
};

// Load the widget configuration
match WidgetConfig::load(&path) {
Ok(Some(widget_config)) => {
new_widget_collection.insert(widget_id, Ok(widget_config));
new_widget_collection.insert(id, Ok(widget_config));
},
Ok(None) => {},
Err(e) => {
// Configuration errors are recorded as error messages and do
// not fail the command
new_widget_collection.insert(widget_id, Err(e.to_string()));
new_widget_collection.insert(id, Err(e.to_string()));
},
};
}
Expand Down
4 changes: 2 additions & 2 deletions crates/deskulpt-plugin-fs/src/commands/append_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ impl PluginCommand for AppendFile {
#[dispatch]
fn run(
&self,
widget_id: String,
id: String,
_plugin: &Self::Plugin,
engine: &EngineInterface,
input: AppendFileInputPayload,
) -> Result<()> {
let path = engine.widget_dir(widget_id.as_str()).join(input.path);
let path = engine.widget_dir(id.as_str()).join(input.path);
let mut file = std::fs::OpenOptions::new().append(true).open(&path)?;
file.write_all(input.content.as_bytes())?;
Ok(())
Expand Down
4 changes: 2 additions & 2 deletions crates/deskulpt-plugin-fs/src/commands/create_dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ impl PluginCommand for CreateDir {
#[dispatch]
fn run(
&self,
widget_id: String,
id: String,
_plugin: &Self::Plugin,
engine: &EngineInterface,
input: CreateDirInputPayload,
) -> Result<()> {
let path = engine.widget_dir(widget_id.as_str()).join(input.path);
let path = engine.widget_dir(id.as_str()).join(input.path);
std::fs::create_dir_all(&path)?;
Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions crates/deskulpt-plugin-fs/src/commands/exists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ impl PluginCommand for Exists {
#[dispatch]
fn run(
&self,
widget_id: String,
id: String,
_plugin: &Self::Plugin,
engine: &EngineInterface,
input: ExistsInputPayload,
) -> Result<bool> {
let path = engine.widget_dir(widget_id).join(input.path);
let path = engine.widget_dir(id).join(input.path);
Ok(path.exists())
}
}
4 changes: 2 additions & 2 deletions crates/deskulpt-plugin-fs/src/commands/is_dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ impl PluginCommand for IsDir {
#[dispatch]
fn run(
&self,
widget_id: String,
id: String,
_plugin: &Self::Plugin,
engine: &EngineInterface,
input: IsDirInputPayload,
) -> Result<bool> {
let path = engine.widget_dir(widget_id.as_str()).join(input.path);
let path = engine.widget_dir(id.as_str()).join(input.path);
Ok(path.is_dir())
}
}
4 changes: 2 additions & 2 deletions crates/deskulpt-plugin-fs/src/commands/is_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ impl PluginCommand for IsFile {
#[dispatch]
fn run(
&self,
widget_id: String,
id: String,
_plugin: &Self::Plugin,
engine: &EngineInterface,
input: IsFileInputPayload,
) -> Result<bool> {
let path = engine.widget_dir(widget_id.as_str()).join(input.path);
let path = engine.widget_dir(id.as_str()).join(input.path);
Ok(path.is_file())
}
}
4 changes: 2 additions & 2 deletions crates/deskulpt-plugin-fs/src/commands/read_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ impl PluginCommand for ReadFile {
#[dispatch]
fn run(
&self,
widget_id: String,
id: String,
_plugin: &Self::Plugin,
engine: &EngineInterface,
input: ReadFileInputPayload,
) -> Result<String> {
let path = engine.widget_dir(widget_id.as_str()).join(input.path);
let path = engine.widget_dir(id.as_str()).join(input.path);
let content = std::fs::read_to_string(&path)?;
Ok(content)
}
Expand Down
4 changes: 2 additions & 2 deletions crates/deskulpt-plugin-fs/src/commands/remove_dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ impl PluginCommand for RemoveDir {
#[dispatch]
fn run(
&self,
widget_id: String,
id: String,
_plugin: &Self::Plugin,
engine: &EngineInterface,
input: RemoveDirInputPayload,
) -> Result<()> {
let path = engine.widget_dir(widget_id.as_str()).join(input.path);
let path = engine.widget_dir(id.as_str()).join(input.path);
std::fs::remove_dir_all(&path)?;
Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions crates/deskulpt-plugin-fs/src/commands/remove_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ impl PluginCommand for RemoveFile {
#[dispatch]
fn run(
&self,
widget_id: String,
id: String,
_plugin: &Self::Plugin,
engine: &EngineInterface,
input: RemoveFileInputPayload,
) -> Result<()> {
let path = engine.widget_dir(widget_id.as_str()).join(input.path);
let path = engine.widget_dir(id.as_str()).join(input.path);
std::fs::remove_file(&path)?;
Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions crates/deskulpt-plugin-fs/src/commands/write_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ impl PluginCommand for WriteFile {
#[dispatch]
fn run(
&self,
widget_id: String,
id: String,
_plugin: &Self::Plugin,
engine: &EngineInterface,
input: WriteFileInputPayload,
) -> Result<()> {
let path = engine.widget_dir(widget_id.as_str()).join(input.path);
let path = engine.widget_dir(id.as_str()).join(input.path);
std::fs::write(&path, input.content)?;
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion crates/deskulpt-plugin-sys/src/commands/get_system_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl PluginCommand for GetSystemInfo {
#[dispatch]
fn run(
&self,
_widget_id: String,
_id: String,
plugin: &Self::Plugin,
_engine: &EngineInterface,
input: (),
Expand Down
4 changes: 2 additions & 2 deletions crates/deskulpt-plugin/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ pub trait PluginCommand {
///
/// Other available information include:
///
/// - `widget_id` is the ID of the widget that triggered the command.
/// - `id` is the ID of the widget that triggered the command.
/// - `plugin` provides a reference back to the plugin that the command is
/// running on. This is useful when the plugin carries some state that the
/// command needs to access.
/// - `engine` provides an interface for interacting with the Deskulpt
/// engine. See [`EngineInterface`] for available methods.
fn run(
&self,
widget_id: String,
id: String,
plugin: &Self::Plugin,
engine: &EngineInterface,
input: serde_json::Value,
Expand Down
4 changes: 2 additions & 2 deletions crates/deskulpt-plugin/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ impl EngineInterface {
/// This method is a temporary implementation. The final implementation
/// should use IPC to communicate with the Deskulpt core to get the widget
/// directory.
pub fn widget_dir<S: AsRef<str>>(&self, widget_id: S) -> PathBuf {
pub fn widget_dir<S: AsRef<str>>(&self, id: S) -> PathBuf {
self.app_handle
.path()
.resource_dir()
.unwrap()
.join("widgets")
.join(widget_id.as_ref())
.join(id.as_ref())
}
}
6 changes: 3 additions & 3 deletions crates/deskulpt-plugin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ pub fn call_plugin<P: Plugin>(
app_handle: AppHandle,
plugin: &P,
command: &str,
widget_id: String,
id: String,
payload: Option<serde_json::Value>,
) -> Result<serde_json::Value> {
let engine = EngineInterface::new(app_handle);

for plugin_command in plugin.commands() {
if plugin_command.name() == command {
return plugin_command.run(
widget_id,
id,
plugin,
&engine,
payload.unwrap_or(serde_json::Value::Null),
Expand Down Expand Up @@ -142,7 +142,7 @@ macro_rules! register_commands {
/// #[dispatch]
/// fn run(
/// &self,
/// _widget_id: String,
/// _id: String,
/// _plugin: &Self::Plugin,
/// _engine: &EngineInterface,
/// input: InputPayload, // Custom deserializable input type
Expand Down
Loading