Cocoa: Standardize and Simplify NS constants, Merge branch 'cocoa/const'

This commit is contained in:
rich2014 2023-11-21 23:32:22 +08:00
parent 57f76d3946
commit 851cd68564
16 changed files with 215 additions and 113 deletions

View File

@ -5,7 +5,7 @@ uses
agl{%H-},
cocoa_extra{%H-},
cocoacaret{%H-},
cocoaconfig{%H-},
cocoaconst{%H-},
cocoacursor{%H-},
cocoagdiobjects{%H-},
cocoaint{%H-},

View File

@ -48,6 +48,20 @@ const // NSImageScaling values
NSImageScaleNone = 2;
NSImageScaleProportionallyUpOrDown = 3;
type
NSPasteboardType = NSString;
var
NSPasteboardTypeString: NSPasteboardType; cvar; external;
NSPasteboardTypePNG: NSPasteboardType; cvar; external;
NSPasteboardTypeTIFF: NSPasteboardType; cvar; external;
NSPasteboardTypePDF: NSPasteboardType; cvar; external;
NSPasteboardTypeHTML: NSPasteboardType; cvar; external;
NSPasteboardTypeRTF: NSPasteboardType; cvar; external;
NSPasteboardTypeColor: NSPasteboardType; cvar; external;
NSPasteboardTypeFont: NSPasteboardType; cvar; external;
NSPasteboardTypeRuler: NSPasteboardType; cvar; external;
NSPasteboardTypeSound: NSPasteboardType; cvar; external;
type
NSMenuFix = objccategory external (NSMenu)
function itemAtIndex(index: NSInteger): NSMenuItem; message 'itemAtIndex:';

View File

@ -1,33 +0,0 @@
unit CocoaConfig;
{$mode ObjFPC}{$H+}
{$modeswitch objectivec1}
interface
uses
CocoaAll;
const
CocoaBasePPI : Integer = 96; // for compatiblity with LCL 1.8 release. The macOS base is 72ppi
// if set to true, then WS would not assign icons via TCocoaWSForm SetIcon
// The icon would have to be changed manually. By default LCL behaviour is used
CocoaIconUse: Boolean = false;
CocoaToggleBezel : NSBezelStyle = NSRoundedBezelStyle;
CocoaToggleType : NSButtonType = NSPushOnPushOffButton;
CocoaHideFocusNoBorder : Boolean = true;
CocoaUseLocalizedFontName : Boolean = false; // some localized named might be too long to be returned properly by APIs
{$ifdef COCOALOOPHIJACK}
// The flag is set to true once hi-jacked loop is finished (at the end of app)
// The flag is checked in Menus to avoid "double" Cmd+Q menu
LoopHiJackEnded : Boolean = false;
{$endif}
implementation
end.

View File

@ -0,0 +1,169 @@
unit CocoaConst;
{$mode ObjFPC}{$H+}
{$modeswitch objectivec1}
interface
uses
CocoaAll;
const
CocoaBasePPI : Integer = 96; // for compatiblity with LCL 1.8 release. The macOS base is 72ppi
// if set to true, then WS would not assign icons via TCocoaWSForm SetIcon
// The icon would have to be changed manually. By default LCL behaviour is used
CocoaIconUse: Boolean = false;
CocoaToggleBezel : NSBezelStyle = NSRoundedBezelStyle;
CocoaToggleType : NSButtonType = NSPushOnPushOffButton;
CocoaHideFocusNoBorder : Boolean = true;
CocoaUseLocalizedFontName : Boolean = false; // some localized named might be too long to be returned properly by APIs
{$ifdef COCOALOOPHIJACK}
// The flag is set to true once hi-jacked loop is finished (at the end of app)
// The flag is checked in Menus to avoid "double" Cmd+Q menu
LoopHiJackEnded : Boolean = false;
{$endif}
function NSSTR_EMPTY: NSString;
function NSSTR_DARK_NAME: NSString;
function NSSTR_DARK_NAME_VIBRANT: NSString;
function NSSTR_LINE_FEED: NSString;
function NSSTR_CARRIAGE_RETURN: NSString;
function NSSTR_LINE_SEPARATOR: NSString;
function NSSTR_PARAGRAPH_SEPARATOR: NSString;
function NSSTR_KEY_ENTER: NSString;
function NSSTR_KEY_ESC: NSString;
function NSSTR_KEY_EQUALS: NSString;
function NSSTR_KEY_PLUS: NSString;
function NSSTR_TABCONTROL_PREV_ARROW: NSSTRING;
function NSSTR_TABCONTROL_NEXT_ARROW: NSSTRING;
implementation
const
DarkName = 'NSAppearanceNameDarkAqua'; // used in 10.14
DarkNameVibrant = 'NSAppearanceNameVibrantDark'; // used in 10.13
var
_NSSTR_EMPTY: NSString;
_NSSTR_DARK_NAME: NSString;
_NSSTR_DARK_NAME_VIBRANT: NSString;
_NSSTR_LINE_FEED: NSString;
_NSSTR_CARRIAGE_RETURN: NSString;
_NSSTR_LINE_SEPARATOR: NSString;
_NSSTR_PARAGRAPH_SEPARATOR: NSString;
_NSSTR_KEY_ENTER: NSString;
_NSSTR_KEY_ESC: NSString;
_NSSTR_KEY_EQUALS: NSString;
_NSSTR_KEY_PLUS: NSString;
_NSSTR_TABCONTROL_PREV_ARROW: NSSTRING;
_NSSTR_TABCONTROL_NEXT_ARROW: NSSTRING;
function NSSTR_EMPTY: NSString;
begin
Result:= _NSSTR_EMPTY;
end;
function NSSTR_DARK_NAME: NSString;
begin
Result:= _NSSTR_DARK_NAME;
end;
function NSSTR_DARK_NAME_VIBRANT: NSString;
begin
Result:= _NSSTR_DARK_NAME_VIBRANT;
end;
function NSSTR_LINE_FEED: NSString;
begin
Result:= _NSSTR_LINE_FEED;
end;
function NSSTR_CARRIAGE_RETURN: NSString;
begin
Result:= _NSSTR_CARRIAGE_RETURN;
end;
function NSSTR_LINE_SEPARATOR: NSString;
begin
Result:= _NSSTR_LINE_SEPARATOR;
end;
function NSSTR_PARAGRAPH_SEPARATOR: NSString;
begin
Result:= _NSSTR_PARAGRAPH_SEPARATOR;
end;
function NSSTR_KEY_ENTER: NSString;
begin
Result:= _NSSTR_KEY_ENTER;
end;
function NSSTR_KEY_ESC: NSString;
begin
Result:= _NSSTR_KEY_ESC;
end;
function NSSTR_KEY_EQUALS: NSString;
begin
Result:= _NSSTR_KEY_EQUALS;
end;
function NSSTR_KEY_PLUS: NSString;
begin
Result:= _NSSTR_KEY_PLUS;
end;
function NSSTR_TABCONTROL_PREV_ARROW: NSSTRING;
begin
Result:= _NSSTR_TABCONTROL_PREV_ARROW;
end;
function NSSTR_TABCONTROL_NEXT_ARROW: NSSTRING;
begin
Result:= _NSSTR_TABCONTROL_NEXT_ARROW;
end;
initialization
_NSSTR_EMPTY:= NSString.string_;
_NSSTR_DARK_NAME:= NSSTR(DarkName);
_NSSTR_DARK_NAME_VIBRANT:= NSSTR(DarkNameVibrant);
_NSSTR_LINE_FEED:= NSSTR(#10);
_NSSTR_CARRIAGE_RETURN:= NSSTR(#13);
_NSSTR_LINE_SEPARATOR:= NSString.alloc.initWithUTF8String(#$E2#$80#$A8);
_NSSTR_PARAGRAPH_SEPARATOR:= NSString.alloc.initWithUTF8String(#$E2#$80#$A9);
_NSSTR_KEY_ENTER:= NSSTR(#13);
_NSSTR_KEY_ESC:= NSSTR(#27);
_NSSTR_KEY_EQUALS:= NSSTR('=');
_NSSTR_KEY_PLUS:= NSSTR('+');
_NSSTR_TABCONTROL_PREV_ARROW:= NSString.alloc.initWithUTF8String('◀');
_NSSTR_TABCONTROL_NEXT_ARROW:= NSString.alloc.initWithUTF8String('▶');
finalization;
_NSSTR_LINE_SEPARATOR.release;
_NSSTR_PARAGRAPH_SEPARATOR.release;
_NSSTR_TABCONTROL_PREV_ARROW.release;
_NSSTR_TABCONTROL_NEXT_ARROW.release;
end.

View File

@ -33,7 +33,7 @@ uses
// darwin bindings
MacOSAll,
// private
CocoaAll, CocoaConfig, CocoaPrivate, CocoaUtils, CocoaGDIObjects, CocoaCursor,
CocoaAll, CocoaConst, CocoaPrivate, CocoaUtils, CocoaGDIObjects, CocoaCursor,
cocoa_extra, CocoaMenus, CocoaWindows, CocoaScrollers,
CocoaWSClipboard, CocoaTextEdits,
// LCL

View File

@ -290,16 +290,16 @@ begin
aButton.setKeyEquivalentModifierMask(0);
if I = DefaultIndex then
aButton.setKeyEquivalent(NSSTR(#13))
aButton.setKeyEquivalent(NSSTR_KEY_ENTER)
else if I = 0 then
// By default, the first button is the default button. If in our
// case this should not be the case, remove the default status
// from the first button.
aButton.setKeyEquivalent(NSSTR(''));
aButton.setKeyEquivalent(NSSTR_EMPTY);
if Buttons[I]=mrCancel then begin
needsCancel := False;
aButton.setKeyEquivalent(NSSTR(#27));
aButton.setKeyEquivalent(NSSTR_KEY_ESC);
end;
aButton.setTag(Buttons[I]);

View File

@ -12,7 +12,7 @@ uses
// LCL
Forms, Menus, LCLType, Classes, LCLStrConsts,
// LCL Cocoa
CocoaAll, CocoaPrivate, CocoaUtils;
CocoaAll, CocoaPrivate, CocoaUtils, CocoaConst;
type
IMenuItemCallback = interface(ICommonCallBack)
@ -144,9 +144,9 @@ begin
// as a key , +/= is a rare case, both + and = are used as primary keys.
// Shift+= for +
// = for ='
if key.isEqualToString(NSSTR('+')) then begin
if key.isEqualToString(NSSTR_KEY_PLUS) then begin
if (ShiftKeyMask and NSShiftKeyMask)=0 then
key := NSStr('=')
key := NSSTR_KEY_EQUALS
else
ShiftKeyMask := ShiftKeyMask - NSShiftKeyMask;
end;

View File

@ -26,7 +26,7 @@ uses
// rtl+ftl
Types, Classes, SysUtils,
// Libs
MacOSAll, CocoaAll, CocoaUtils, CocoaPrivate;
MacOSAll, CocoaAll, CocoaUtils, CocoaPrivate, CocoaConst;
type
@ -189,9 +189,9 @@ begin
btn.setButtonType(NSMomentaryLightButton);
if isPrev then
btn.setTitle( StrToNSString('◀') )
btn.setTitle( NSSTR_TABCONTROL_PREV_ARROW )
else
btn.setTitle( StrToNSString('▶') );
btn.setTitle( NSSTR_TABCONTROL_NEXT_ARROW );
{$ifdef BOOLFIX}
btn.setBordered_(Ord(false));

View File

@ -29,7 +29,7 @@ uses
Types, Classes, SysUtils,
// Libs
MacOSAll, CocoaAll, CocoaUtils, CocoaGDIObjects,
cocoa_extra, CocoaPrivate,
cocoa_extra, CocoaPrivate, CocoaConst,
// LCL
LCLType;
@ -924,7 +924,7 @@ begin
if not isFirstColumnCheckboxes and isImagesInCell then begin
img := lclGetItemImageAt(row, col);
Result := NSImageAndTextCell(NSImageAndTextCell.alloc).initTextCell(NSSTR(''));
Result := NSImageAndTextCell(NSImageAndTextCell.alloc).initTextCell(NSSTR_EMPTY);
NSImageAndTextCell(Result).drawImage := img; // if "image" is assigned, text won't be drawn :(
Exit;
end;

View File

@ -22,7 +22,7 @@ uses
LCLType, LCLProc, LCLIntf, Graphics, Themes, TmSchema,
customdrawndrawers,
// widgetset
CocoaUtils, CocoaGDIObjects;
CocoaUtils, CocoaGDIObjects, CocoaConst;
type
{ TCocoaThemeServices }
@ -266,7 +266,7 @@ var
begin
if (HdrCell=nil) then
begin
hdrCell := NSTableHeaderCell.alloc.initTextCell(NSSTR(''));
hdrCell := NSTableHeaderCell.alloc.initTextCell(NSSTR_EMPTY);
end;
CellDrawStart(DC, R, cur, nsr);
@ -530,7 +530,7 @@ begin
end;
if cocoaBtn < 0 then Exit; // unsupported button type
btn := NSButtonCell(NSButtonCell.alloc).initTextCell(NSSTR(''));
btn := NSButtonCell(NSButtonCell.alloc).initTextCell(NSSTR_EMPTY);
btn.setButtonType(NSButtonType(cocoaBtn));
SetButtonCellToDetails(btn, Details);
@ -542,7 +542,7 @@ end;
constructor TCocoaThemeServices.Create;
begin
inherited Create;
BtnCell := NSButtonCell.alloc.initTextCell(NSSTR(''));
BtnCell := NSButtonCell.alloc.initTextCell(NSSTR_EMPTY);
BezelToolBar := NSSmallSquareBezelStyle; // can be resized at any size
BezelButton := NSSmallSquareBezelStyle;

View File

@ -7,7 +7,7 @@ interface
uses
classes,
MacOSAll, CocoaAll, Cocoa_Extra,
MacOSAll, CocoaAll, Cocoa_Extra, CocoaConst,
SysUtils, Types, LCLType, LCLProc,
Graphics, GraphType;
@ -154,20 +154,8 @@ function NSEventRawKeyChar(ev: NSEvent): System.WideChar;
function AllocImageRotatedByDegrees(src: NSImage; degrees: double): NSImage;
function AllocCursorFromCursorByDegrees(src: NSCursor; degrees: double): NSCursor;
var
NSSTR_DARK_NAME: NSString;
NSSTR_DARK_NAME_VIBRANT: NSString;
NSSTR_LINE_FEED: NSString;
NSSTR_CARRIAGE_RETURN: NSString;
NSSTR_LINE_SEPARATOR: NSString;
NSSTR_PARAGRAPH_SEPARATOR: NSString;
implementation
const
DarkName = 'NSAppearanceNameDarkAqua'; // used in 10.14
DarkNameVibrant = 'NSAppearanceNameVibrantDark'; // used in 10.13
procedure ApplicationWillShowModal;
begin
// Any place that would attempt to use Cocoa-native modality.
@ -1361,18 +1349,5 @@ begin
img.release;
end;
initialization
NSSTR_DARK_NAME:= NSSTR(DarkName);
NSSTR_DARK_NAME_VIBRANT:= NSSTR(DarkNameVibrant);
NSSTR_LINE_FEED:= NSStr(#10);
NSSTR_CARRIAGE_RETURN:= NSStr(#13);
NSSTR_LINE_SEPARATOR:= StrToNSString(#$E2#$80#$A8, false);
NSSTR_PARAGRAPH_SEPARATOR:= StrToNSString(#$E2#$80#$A9, false);
finalization;
NSSTR_LINE_SEPARATOR.release;
NSSTR_PARAGRAPH_SEPARATOR.release;
end.

View File

@ -24,7 +24,7 @@ uses
// fcl-image
,fpreadpng, fpwritepng, fpimage, fpreadbmp, fpwritebmp
,LCLType
,CocoaUtils;
,CocoaUtils, Cocoa_Extra;
type
TCocoaClipboardDataType = (ccdtText,
@ -81,22 +81,8 @@ type
end;
const
// these constants are available starting MacOSX 10.6
// thus for earlier systems must be redeclared
_NSPasteboardTypeString : NSString = nil;
_NSPasteboardTypePNG : NSString = nil;
_NSPasteboardTypeTiff : NSString = nil;
implementation
procedure InitConst;
begin
_NSPasteboardTypeString := NSSTR('public.utf8-plain-text');
_NSPasteboardTypePNG := NSSTR('public.png');
_NSPasteboardTypeTiff := NSSTR('public.tiff');
end;
{ TCocoaWSClipboard }
constructor TCocoaWSClipboard.Create;
@ -433,20 +419,17 @@ begin
case AMimeType of
'text/plain':
begin
//hack: the name of constants is a hack
// should be replaced with either weaklinking
// or dynamic loading (dlsym)
lNSStr := NSSTR('public.utf8-plain-text'); // NSPasteboardTypeString; // commented out for OSX < 10.6 see #33672
lNSStr := NSPasteboardTypeString;
lDataType := ccdtText;
end;
'image/png':
begin
lNSStr := NSSTR('public.png'); // NSPasteboardTypePNG
lNSStr := NSPasteboardTypePNG;
lDataType := ccdtCocoaStandard;
end;
'image/bmp':
begin
lNSStr := NSSTR('public.png'); // NSPasteboardTypePNG
lNSStr := NSPasteboardTypePNG;
lDataType := ccdtBitmap;
end;
else
@ -470,7 +453,7 @@ end;
function TCocoaWSClipboard.CocoaTypeToMimeType(AType: NSString): string;
begin
// "default" types must be mapped to a default LCL mime-type
if AType.isEqualToString(_NSPasteboardTypeString) then
if AType.isEqualToString(NSPasteboardTypeString) then
Result := 'text/plain'
else
Result := NSStringToString(AType);
@ -490,8 +473,4 @@ begin
CocoaFormat.release;
end;
initialization
InitConst;
end.

View File

@ -10,7 +10,7 @@ interface
uses
Types, Classes, Controls, SysUtils,
WSControls, LCLType, LCLMessageGlue, LMessages, LCLProc, LCLIntf, Graphics, Forms,
CocoaAll, CocoaInt, CocoaConfig, CocoaPrivate, CocoaUtils,
CocoaAll, CocoaInt, CocoaConst, CocoaPrivate, CocoaUtils,
CocoaGDIObjects, CocoaCursor, CocoaCaret, CocoaScrollers, cocoa_extra;
type

View File

@ -30,7 +30,7 @@ uses
// Widgetset
WSForms, WSLCLClasses, LCLMessageGlue,
// LCL Cocoa
CocoaInt, CocoaConfig, CocoaPrivate, CocoaUtils, CocoaWSCommon, CocoaMenus,
CocoaInt, CocoaConst, CocoaPrivate, CocoaUtils, CocoaWSCommon, CocoaMenus,
CocoaGDIObjects,
CocoaWindows, CocoaScrollers, cocoa_extra;
@ -642,7 +642,7 @@ begin
SetWindowButtonState(NSWindowZoomButton, (biMaximize in ABorderIcons) and (ABorderStyle in [bsSizeable, bsSizeToolWin]), (ABorderStyle in [bsSingle, bsSizeable]) and (biSystemMenu in ABorderIcons));
SetWindowButtonState(NSWindowCloseButton, True, (ABorderStyle <> bsNone) and (biSystemMenu in ABorderIcons));
if not CocoaConfig.CocoaIconUse then
if not CocoaConst.CocoaIconUse then
begin
btn := AWindow.standardWindowButton(NSWindowDocumentIconButton);
url := nil;
@ -1048,7 +1048,7 @@ var
trg : NSImage;
btn : NSButton;
begin
if CocoaConfig.CocoaIconUse then Exit;
if CocoaConst.CocoaIconUse then Exit;
if not AForm.HandleAllocated then Exit;
win := TCocoaWindowContent(AForm.Handle).lclOwnWindow;

View File

@ -33,7 +33,7 @@ uses
// Widgetset
WSStdCtrls, WSLCLClasses,
// LCL Cocoa
CocoaConfig, CocoaWSCommon, CocoaPrivate, CocoaUtils, CocoaGDIObjects, CocoaButtons,
CocoaConst, CocoaWSCommon, CocoaPrivate, CocoaUtils, CocoaGDIObjects, CocoaButtons,
CocoaTables, CocoaTextEdits, CocoaScrollers, Cocoa_Extra;
type
@ -1432,8 +1432,6 @@ var
ns : NSString;
idx : integer;
ro : Boolean;
const
LFSTR = #10;
begin
ns:=FTextView.string_;
idx:=0;
@ -1455,7 +1453,7 @@ begin
FTextView.setSelectedRange(rng);
if (rng.location>=ns.length) and (st=ced) and (ns.length>0) then
FTextView.insertText( NSString.stringWithUTF8String( LFSTR ));
FTextView.insertText( NSSTR_LINE_FEED );
if S<>'' then
begin
@ -1463,7 +1461,7 @@ begin
end;
dec(FTextView.supressTextChangeEvent);
FTextView.insertText( NSString.stringWithUTF8String( LFSTR ));
FTextView.insertText( NSSTR_LINE_FEED );
if not ro then FTextView.setEditable(ro);

View File

@ -2614,9 +2614,9 @@ end;"/>
<UnitName Value="AllLCLIntfUnits"/>
</Item529>
<Item530>
<Filename Value="cocoa/cocoaconfig.pas"/>
<Filename Value="cocoa/cocoaconst.pas"/>
<AddToUsesPkgSection Value="False"/>
<UnitName Value="CocoaConfig"/>
<UnitName Value="CocoaConst"/>
</Item530>
<Item531>
<Filename Value="cocoa/cocoacursor.pas"/>