Skip to content

Commit

Permalink
Added "HideOnFocusLost"
Browse files Browse the repository at this point in the history
  • Loading branch information
flyingpie committed May 12, 2024
1 parent bbcd6e8 commit 0bfe1b5
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 13 deletions.
37 changes: 29 additions & 8 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,27 @@
- Added support for multiple configuration file locations (see docs for more info).
- Moved default log location to temp directory.
- Added "Open logs" button to tray icon.
- Added "HideOnFocusLost", which controls whether an app should be toggled out if another app gains focus.

Globally, for all apps:
```json
{
"HideOnFocusLost": true | false
}
```

Or per app:
```json
{
"Apps": [
{
"Name": "Terminal",
"HideOnFocusLost": true | false
},
...
]
}
```

## [2.0.8] / 2024-04-28
- Automated Scoop and WinGet manifest generation.
Expand All @@ -19,15 +40,15 @@
![image](https://github.com/flyingpie/windows-terminal-quake/assets/1295673/ee95e8bd-b6b2-4c48-a680-60e36a4398e1)

Globally, for all apps:
```jsonc
```json
{
"Opacity": 0-100,
"TaskBarIconVisibility": "AlwaysHidden | AlwaysVisible"
}
```

Or per app:
```jsonc
```json
{
"Apps": [
{
Expand All @@ -47,7 +68,7 @@ Or per app:
![wtq-sizing-01](https://github.com/flyingpie/windows-terminal-quake/assets/1295673/0f0a8f81-b0d5-4256-a15d-ac384e6386a1)

Globally, for all apps:
```jsonc
```json
{
// Horizontal screen coverage, as a percentage (defaults to 95).
"HorizontalScreenCoverage": 95,
Expand All @@ -61,7 +82,7 @@ Globally, for all apps:
```

Or per app:
```jsonc
```json
{
"Apps": [
{
Expand All @@ -88,7 +109,7 @@ Initial support for auto-starting apps.
The configuration file has been simplified.

Old syntax:
```jsonc
```json
"Apps": [
{
"Name": "Terminal",
Expand All @@ -106,7 +127,7 @@ Old syntax:
The "**ProcessName**"-property is optional for processes where they are the same.

New syntax:
```jsonc
```json
"Apps": [
{
"Name": "Terminal",
Expand All @@ -130,7 +151,7 @@ Always starts a new process, specifically to be used by WTQ. Meant for apps that
Attaches whatever app has focus, when the hot key is pressed. Keeps the app attached until WTQ is closed.

The mode can be specified per app (note that "FindOrStart" is the default:
```jsonc
```json
"Apps": [
{
"Name": "Terminal",
Expand All @@ -147,7 +168,7 @@ The mode can be specified per app (note that "FindOrStart" is the default:

The setting is available at the root config level, and can be overridden per application.

```jsonc
```json
{
"PreferMonitor": "WithCursor", // WithCursor | Primary | AtIndex
"MonitorIndex": 0,
Expand Down
3 changes: 3 additions & 0 deletions src/10-Core/Wtq/Configuration/WtqAppOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public sealed class WtqAppOptions
[Required]
public string? FileName { get; set; }

/// <inheritdoc cref="WtqOptions.HideOnFocusLost"/>
public bool? HideOnFocusLost { get; init; }

/// <inheritdoc cref="WtqOptions.HorizontalAlign"/>
public HorizontalAlign? HorizontalAlign { get; set; }

Expand Down
14 changes: 14 additions & 0 deletions src/10-Core/Wtq/Configuration/WtqOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ public sealed class WtqOptions
public AttachMode AttachMode { get; init; }
= AttachMode.FindOrStart;

/// <summary>
/// Whether the app should be toggled out when another app gets focus.<br/>
/// Defaults to "true".
/// </summary>
public bool HideOnFocusLost { get; init; }
= true;

/// <summary>
/// Where to position an app on the chosen monitor, horizontally.<br/>
/// Defaults to <see cref="HorizontalAlign.Center"/>.
Expand Down Expand Up @@ -80,6 +87,13 @@ public sealed class WtqOptions
public float VerticalScreenCoverage { get; init; }
= 95f;

public bool GetHideOnFocusLostForApp(WtqAppOptions opts)
{
Guard.Against.Null(opts);

return opts.HideOnFocusLost ?? HideOnFocusLost;
}

public HorizontalAlign GetHorizontalAlignForApp(WtqAppOptions opts)
{
Guard.Against.Null(opts);
Expand Down
16 changes: 11 additions & 5 deletions src/10-Core/Wtq/WtqService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ namespace Wtq;

public sealed class WtqService(
ILogger<WtqService> log,
IOptionsMonitor<WtqOptions> opts,
WtqAppMonitorService appMon,
IWtqAppRepo appRepo,
IWtqBus bus)
: IHostedService
{
private readonly WtqAppMonitorService _appMon = appMon ?? throw new ArgumentNullException(nameof(appMon));
private readonly IWtqAppRepo _appRepo = appRepo ?? throw new ArgumentNullException(nameof(appRepo));
private readonly IWtqBus _bus = bus ?? throw new ArgumentNullException(nameof(bus));
private readonly ILogger<WtqService> _log = log ?? throw new ArgumentNullException(nameof(log));
private readonly WtqAppMonitorService _appMon = Guard.Against.Null(appMon);
private readonly IWtqAppRepo _appRepo = Guard.Against.Null(appRepo);
private readonly IWtqBus _bus = Guard.Against.Null(bus);
private readonly ILogger<WtqService> _log = Guard.Against.Null(log);
private readonly IOptionsMonitor<WtqOptions> _opts = Guard.Against.Null(opts);

private WtqApp? _lastOpen;
private WtqApp? _open;
Expand All @@ -38,7 +40,11 @@ public Task StopAsync(CancellationToken cancellationToken)

private async Task HandleAppFocusEventAsync(WtqAppFocusEvent ev)
{
if (ev.App != null && ev.App == _open && !ev.GainedFocus)
// If focus moved to a different window, toggle out the current one (if there is an active app, and it's configured as such).
if (ev.App != null &&
ev.App == _open &&
!ev.GainedFocus &&
_opts.CurrentValue.GetHideOnFocusLostForApp(ev.App.Options))
{
await _open.CloseAsync().ConfigureAwait(false);
_lastOpen = _open;
Expand Down

0 comments on commit 0bfe1b5

Please sign in to comment.