Skip to content

Commit

Permalink
Have the red button hide the application on macOS (#131)
Browse files Browse the repository at this point in the history
  • Loading branch information
lighterowl authored Feb 6, 2025
1 parent 7892872 commit 4d8d397
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 13 deletions.
51 changes: 39 additions & 12 deletions main.pas
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{*************************************************************************************
This file is part of Transmission Remote GUI.
Copyright (c) 2008-2019 by Yury Sidorov and Transmission Remote GUI working group.
Copyright (c) 2023-2024 by Daniel Kamil Kozar
Copyright (c) 2023-2025 by Daniel Kamil Kozar
Transmission Remote GUI is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -43,7 +43,7 @@ interface
lclintf,
{$endif windows}
{$ifdef darwin}
MacOSThemeDetect,
MacOSThemeDetect, CocoaConfig,
{$endif}
Graphics, Dialogs, ComCtrls, Menus, ActnList, LCLVersion,
httpsend, StdCtrls, fpjson, jsonparser, ExtCtrls, rpc, syncobjs, variants, varlist, IpResolver,
Expand Down Expand Up @@ -801,6 +801,9 @@ TMainForm = class(TForm)
{$ifdef windows}
procedure SetUpWindowsHotKey;
{$endif windows}
{$ifdef darwin}
procedure CocoaQuitApp(Data: PtrInt);
{$endif darwin}
end;

function AppName: string;
Expand Down Expand Up @@ -989,6 +992,18 @@ procedure TMainForm.SetUpWindowsHotKey;

{$endif windows}

{$ifdef darwin}
procedure TMainForm.CocoaQuitApp(Data: PtrInt);
begin
{ called by the OS when "Quit" is selected from the application's right-click
menu in the dock, or when the user is logging out / machine is shutting
down - in which case EndSession is called too so we end up calling it twice
which isn't really a problem. }
ApplicationPropertiesEndSession(self);
Application.Terminate;
end;
{$endif darwin}

function IsHash(Hash: String): boolean;
var i: integer;
begin
Expand Down Expand Up @@ -1612,6 +1627,8 @@ procedure TMainForm.FormCreate(Sender: TObject);

RegisterURLHandler(@AddTorrentFile);
MacOSThemeDetect.Callback := @OnThemeChanged;
CocoaConfigApplication.events.onQuitApp := @CocoaQuitApp;
miTorrent.Remove(miExit);
{$endif darwin}

Application.Title:=AppName + ' v' + AppVersion;
Expand Down Expand Up @@ -2238,12 +2255,13 @@ procedure TMainForm.acOptionsExecute(Sender: TObject);
edRefreshInterval.Value:=Ini.ReadInteger('Interface', 'RefreshInterval', 5);
edRefreshIntervalMin.Value:=Ini.ReadInteger('Interface', 'RefreshIntervalMin', 20);
cbCalcAvg.Checked:=FCalcAvg;
{$ifndef darwin}
cbTrayMinimize.Checked:=Ini.ReadBool('Interface', 'TrayMinimize', True);
{$ifdef darwin}
cbTrayMinimize.Visible := False;
cbTrayClose.Visible := False;
{$else}
cbTrayMinimize.Enabled:=False;
{$endif}
cbTrayMinimize.Checked:=Ini.ReadBool('Interface', 'TrayMinimize', True);
cbTrayClose.Checked:=Ini.ReadBool('Interface', 'TrayClose', False);
{$endif}
cbTrayIconAlways.Checked:=Ini.ReadBool('Interface', 'TrayIconAlways', True);
cbTrayNotify.Checked:=Ini.ReadBool('Interface', 'TrayNotify', True);

Expand All @@ -2270,10 +2288,13 @@ procedure TMainForm.acOptionsExecute(Sender: TObject);
Ini.WriteInteger('Interface', 'RefreshInterval', edRefreshInterval.Value);
Ini.WriteInteger('Interface', 'RefreshIntervalMin', edRefreshIntervalMin.Value);
Ini.WriteBool('Interface', 'CalcAvg', cbCalcAvg.Checked);
{$ifndef darwin}
{$ifdef darwin}
Ini.DeleteKey('Interface', 'TrayMinimize');
Ini.DeleteKey('Interface', 'TrayClose');
{$else}
Ini.WriteBool('Interface', 'TrayMinimize', cbTrayMinimize.Checked);
{$endif}
Ini.WriteBool('Interface', 'TrayClose', cbTrayClose.Checked);
{$endif}
Ini.WriteBool('Interface', 'TrayIconAlways', cbTrayIconAlways.Checked);
Ini.WriteBool('Interface', 'TrayNotify', cbTrayNotify.Checked);

Expand Down Expand Up @@ -2913,9 +2934,12 @@ function TMainForm.DoAddTorrent(const FileName: Utf8String): boolean;
procedure TMainForm.UpdateTray;
begin
TrayIcon.Visible:=not IsUnity and
(Ini.ReadBool('Interface', 'TrayIconAlways', True) or
(Ini.ReadBool('Interface', 'TrayIconAlways', True)
{$ifndef darwin}
or
((WindowState = wsMinimized) and Ini.ReadBool('Interface', 'TrayMinimize', True) ) or
(not Self.Visible and Ini.ReadBool('Interface', 'TrayClose', False) )
{$endif darwin}
);

{$ifdef darwin}
Expand Down Expand Up @@ -4096,6 +4120,7 @@ procedure TMainForm.ApplicationPropertiesEndSession(Sender: TObject);
begin
DeleteFileUTF8(FRunFileName);
BeforeCloseApp;
MainForm.OnClose:=nil;
end;

procedure TMainForm.ApplicationPropertiesIdle(Sender: TObject; var Done: Boolean);
Expand Down Expand Up @@ -4731,10 +4756,12 @@ procedure TMainForm.FilterTimerTimer(Sender: TObject);

procedure TMainForm.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
if Ini.ReadBool('Interface', 'TrayClose', False) then begin
{$ifdef darwin}
CloseAction:=caMinimize;
Application.Minimize; { calls NSApplication.hide }
CloseAction:=caNone;
exit;
{$else}
if Ini.ReadBool('Interface', 'TrayClose', False) then begin
{$ifdef linux}
if IsUnity then
CloseAction:=caMinimize
Expand All @@ -4745,10 +4772,10 @@ procedure TMainForm.FormClose(Sender: TObject; var CloseAction: TCloseAction);
HideApp;
UpdateTray;
end;
{$endif darwin}
exit;
end;
BeforeCloseApp;
{$endif}
end;

procedure TMainForm.PageInfoChange(Sender: TObject);
Expand Down
1 change: 0 additions & 1 deletion transgui.lpi
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
<SessionStorage Value="InProjectDir"/>
<AutoCreateForms Value="False"/>
<Scaled Value="True"/>
<UseAppBundle Value="False"/>
<UseXPManifest Value="True"/>
<XPManifest>
<DpiAware Value="True"/>
Expand Down

0 comments on commit 4d8d397

Please sign in to comment.