Repository navigation
LR-BEHAV-BG-TASKS: on macOS the Dock icon shows how many jobs are running (stacked on #3208) #3219
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
Open
nledford
wants to merge
5
commits into
storytold:main
Choose a base branch
from
nledford:mac-long-jobs-dock-badge
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
04221b4
LR-BEHAV-BG-TASKS: a running job keeps the Mac awake and the app out …
nledford 3636d64
LR-BEHAV-BG-TASKS: list every housekeeping kind the keep-awake rule s…
nledford 1e5a098
LR-BEHAV-BG-TASKS: a test that a holding job asks for a repaint withi…
nledford d85adc4
LR-BEHAV-BG-TASKS: the Dock icon shows how many jobs are running
nledford f519240
LR-BEHAV-BG-TASKS: the Dock count's tests cover every housekeeping ki…
nledford File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| //! The number of running jobs on the Dock icon (macOS). | ||
| //! | ||
| //! The UI says how many jobs the person started are running (`Services::dock_badge`, from | ||
| //! `Activity::running_jobs`); this shows it as the badge of the app's Dock tile, and clears it at zero, so an | ||
| //! export in a minimized window can be seen from the Dock. A badge is text on the icon: `NSDockTile` has no safe | ||
| //! drawing path for a progress bar without a custom view, and the badge needs no `unsafe`. | ||
|
|
||
| use objc2::MainThreadMarker; | ||
| use objc2_app_kit::NSApplication; | ||
| use objc2_foundation::NSString; | ||
|
|
||
| /// The text of the badge for `running` jobs: nothing at zero. | ||
| pub fn label(running: usize) -> Option<String> { | ||
| (running > 0).then(|| running.to_string()) | ||
| } | ||
|
|
||
| /// Show `running` on the Dock icon. AppKit is main-thread only: from another thread nothing is shown. | ||
| pub fn show(running: usize) { | ||
| let Some(mtm) = MainThreadMarker::new() else { return }; | ||
| let tile = NSApplication::sharedApplication(mtm).dockTile(); | ||
| match label(running) { | ||
| Some(text) => tile.setBadgeLabel(Some(&NSString::from_str(&text))), | ||
| None => tile.setBadgeLabel(None), | ||
| } | ||
| } | ||
|
|
||
| /// The host service for `Services::dock_badge`. | ||
| pub fn service() -> lightcraft_ui_egui::DockBadgeFn { | ||
| Box::new(show) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn the_badge_is_the_count_and_empty_when_idle() { | ||
| assert_eq!(label(0), None); | ||
| assert_eq!(label(1).as_deref(), Some("1")); | ||
| assert_eq!(label(12).as_deref(), Some("12")); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| //! Keep running while a long job does (macOS). | ||
| //! | ||
| //! While an export, import or other job the person started is in flight, macOS must neither put the Mac to idle | ||
| //! sleep nor App-Nap the window: a minimized or hidden app is throttled, and a long export of a library on a share | ||
| //! stalls. The UI says when that changes (`Services::keep_awake`, from `Activity::wake_reason`); this holds the | ||
| //! matching `NSProcessInfo` activity (user initiated, idle system sleep disabled) whose reason names the job, which | ||
| //! `pmset -g assertions` then shows. | ||
| //! | ||
| //! The activity is held with `performActivityWithOptions:reason:usingBlock:`, which is safe to call: a helper thread | ||
| //! sits inside the block until its [`Hold`] is dropped, and AppKit ends the activity when the block returns. | ||
| //! `beginActivityWithOptions:reason:` would need `endActivity:`, an `unsafe` method, and this crate forbids `unsafe`. | ||
| //! Activities belong to the process, not to the thread that began them. | ||
|
|
||
| use std::sync::{Arc, Mutex, PoisonError, mpsc}; | ||
|
|
||
| use block2::RcBlock; | ||
| use objc2_foundation::{NSActivityOptions, NSProcessInfo, NSString}; | ||
|
|
||
| /// A running activity: dropping it ends it. | ||
| pub struct Hold { | ||
| /// The helper thread waits for this to disconnect. | ||
| _release: mpsc::Sender<()>, | ||
| } | ||
|
|
||
| /// Begin an activity named `reason`. `previous` ends only once this one has begun, so the Mac is never left | ||
| /// unprotected when the job named in the reason changes. If the helper thread can't start, `previous` is returned | ||
| /// still held (the Mac stays protected, under the old reason), or `None` when there was none. | ||
| pub fn begin(reason: &str, previous: Option<Hold>) -> Option<Hold> { | ||
| let (release, wait) = mpsc::channel::<()>(); | ||
| let reason = reason.to_owned(); | ||
| // shared with the thread so that a thread that never starts leaves it here | ||
| let previous = Arc::new(Mutex::new(previous)); | ||
| let handed = previous.clone(); | ||
| let spawned = std::thread::Builder::new().name("keep-awake".into()).spawn(move || { | ||
| // (an `NSString` is not `Send`: made here) | ||
| let reason = NSString::from_str(&reason); | ||
| let held = RcBlock::new(move || { | ||
| // begun: the one before it can go | ||
| drop(handed.lock().unwrap_or_else(PoisonError::into_inner).take()); | ||
| // (Err: the hold was dropped, which is the end) | ||
| let _ = wait.recv(); | ||
| }); | ||
| NSProcessInfo::processInfo().performActivityWithOptions_reason_usingBlock( | ||
| NSActivityOptions::UserInitiated | NSActivityOptions::IdleSystemSleepDisabled, | ||
| &reason, | ||
| &held, | ||
| ); | ||
| }); | ||
| match spawned { | ||
| Ok(_) => Some(Hold { _release: release }), | ||
| Err(e) => { | ||
| log::warn!("could not keep the Mac awake for a running job: {e}"); | ||
| previous.lock().unwrap_or_else(PoisonError::into_inner).take() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// The host service: holds one activity while the UI says a job needs it. | ||
| pub fn service() -> lightcraft_ui_egui::KeepAwakeFn { | ||
| let mut hold: Option<Hold> = None; | ||
| Box::new(move |reason| { | ||
| hold = match reason { | ||
| Some(reason) => begin(reason, hold.take()), | ||
| None => None, | ||
| }; | ||
| }) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use std::time::{Duration, Instant}; | ||
|
|
||
| fn assertions() -> String { | ||
| let out = std::process::Command::new("pmset").args(["-g", "assertions"]).output().expect("pmset runs"); | ||
| String::from_utf8_lossy(&out.stdout).into_owned() | ||
| } | ||
|
|
||
| fn wait_for(reason: &str, present: bool) -> bool { | ||
| let t0 = Instant::now(); | ||
| while t0.elapsed() < Duration::from_secs(10) { | ||
| if assertions().contains(reason) == present { | ||
| return true; | ||
| } | ||
| std::thread::sleep(Duration::from_millis(100)); | ||
| } | ||
| false | ||
| } | ||
|
|
||
| #[test] | ||
| fn a_hold_shows_in_the_power_assertions_until_it_is_dropped() { | ||
| let reason = format!("LightCraft test hold {}", std::process::id()); | ||
| let hold = begin(&reason, None).expect("the helper thread starts"); | ||
| assert!(wait_for(&reason, true), "pmset lists the activity while the hold lives"); | ||
| drop(hold); | ||
| assert!(wait_for(&reason, false), "pmset stops listing it once the hold is dropped"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn a_new_reason_replaces_the_old_one() { | ||
| let first = format!("LightCraft first hold {}", std::process::id()); | ||
| let second = format!("LightCraft second hold {}", std::process::id()); | ||
| let a = begin(&first, None); | ||
| assert!(wait_for(&first, true)); | ||
| let b = begin(&second, a); | ||
| assert!(wait_for(&second, true)); | ||
| assert!(wait_for(&first, false), "the earlier one ends once the later one has begun"); | ||
| drop(b); | ||
| assert!(wait_for(&second, false)); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.