Skip to content

Commit

Permalink
[py][mpd] rework
Browse files Browse the repository at this point in the history
  • Loading branch information
aslpavel committed Nov 12, 2024
1 parent 8469a5e commit e52135f
Show file tree
Hide file tree
Showing 5 changed files with 423 additions and 316 deletions.
30 changes: 15 additions & 15 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ resolver = "2"
[workspace.package]
authors = ["Pavel Aslanov <[email protected]>"]
edition = "2021"
version = "0.24.1"
version = "0.24.2"
repository = "https://github.com/aslpavel/sweep-rs"

[workspace.dependencies]
Expand Down
59 changes: 41 additions & 18 deletions sweep-lib/src/sweep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,10 @@ enum SweepWindowRequest<H> {
/// Request generated by [Sweep] type
enum SweepRequest<H> {
Terminate,
WindowSwitch(Either<Box<dyn Window>, WindowId>),
WindowSwitch {
window: Either<Box<dyn Window>, WindowId>,
created: oneshot::Sender<bool>,
},
WindowPop,
WindowRequest {
uid: Option<WindowId>,
Expand Down Expand Up @@ -375,8 +378,13 @@ where
}

/// Create new state and put on the top of the stack of active states
pub fn window_switch(&self, uid: WindowId) {
self.send_request(SweepRequest::WindowSwitch(Either::Right(uid)))
pub async fn window_switch(&self, uid: WindowId) -> Result<bool, Error> {
let (send, recv) = oneshot::channel();
self.send_request(SweepRequest::WindowSwitch {
window: Either::Right(uid),
created: send,
});
Ok(recv.await?)
}

/// Remove state at the top of the stack and active one below it
Expand Down Expand Up @@ -410,6 +418,7 @@ where
None,
Arc::new({
let send = std::sync::Mutex::new(Some(send)); // one shot is moved on send
let uid = uid.clone();
move |event| {
if let SweepEvent::Select { items, .. } = event {
if let Some(send) = send.with_mut(|send| send.take()) {
Expand All @@ -425,7 +434,14 @@ where
}),
);
window.haystack_extend(items.into_iter().collect());
self.send_request(SweepRequest::WindowSwitch(Either::Left(Box::new(window))));
let (send_switch, recv_switch) = oneshot::channel();
self.send_request(SweepRequest::WindowSwitch {
window: Either::Left(Box::new(window)),
created: send_switch,
});
if !recv_switch.await? {
anyhow::bail!("window with this uid already exits: {uid:?}");
}
Ok(recv.await.unwrap_or(Vec::new()))
}

Expand Down Expand Up @@ -739,8 +755,7 @@ where
let sweep = sweep.clone();
async move {
let uid = params.take(0, "uid")?;
sweep.window_switch(uid);
Ok(Value::Null)
Ok(sweep.window_switch(uid).await?)
}
}
});
Expand Down Expand Up @@ -2097,25 +2112,33 @@ where
use SweepRequest::*;
let window_event = match request {
Terminate => return Ok(TerminalAction::Quit(())),
WindowSwitch(Either::Left(window)) => WindowAction::Open { window },
WindowSwitch(Either::Right(uid)) => {
WindowSwitch { window, created } => {
let uid = window.as_ref().either(|win| win.uid(), |uid| &uid);
if window_stack.window_position(&uid).is_some() {
_ = created.send(false);
WindowAction::Switch {
uid,
uid: uid.clone(),
args: Value::Null,
close: false,
}
} else {
let window = Box::new(SweepWindow::new_from_options(
SweepOptions {
window_uid: uid.clone(),
..options.clone()
_ = created.send(true);
let window = window.either(
|win| Ok::<_, Error>(win),
|uid| {
let win = Box::new(SweepWindow::new_from_options(
SweepOptions {
window_uid: uid.clone(),
..options.clone()
},
haystack_context.clone(),
term.waker(),
Some(window_dispatch.create(uid)?),
event_handler_default.clone(),
));
Ok(win)
},
haystack_context.clone(),
term.waker(),
Some(window_dispatch.create(uid)?),
event_handler_default.clone(),
));
)?;
WindowAction::Open { window }
}
}
Expand Down
Loading

0 comments on commit e52135f

Please sign in to comment.