mirror of
https://github.com/ankitects/anki.git
synced 2025-09-18 14:02:21 -04:00
switch AnkiHelper to Swift
More pleasant to work with than ObjectiveC, which will help with the following commit. Swift libraries weren't added to macOS until 10.14.4, so theme autodetection will fail on 10.14.0-10.14.3. The Qt6 build will have its minimum version bumped to 10.14.4; the Qt5 build will remain on 10.13.4. Bazel's rules_swift doesn't currently support building Swift dylibs, so we need to invoke swiftc directly via a genrule().
This commit is contained in:
parent
924e16879f
commit
d9c8addbc1
9 changed files with 82 additions and 53 deletions
|
@ -1,34 +0,0 @@
|
|||
// Copyright: Ankitects Pty Ltd and contributors
|
||||
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
||||
|
||||
@import Foundation;
|
||||
@import AppKit;
|
||||
|
||||
/// Force our app to be either light or dark mode.
|
||||
void set_darkmode_enabled(BOOL enabled) {
|
||||
NSAppearance *appearance;
|
||||
if (enabled) {
|
||||
appearance = [NSAppearance appearanceNamed:NSAppearanceNameDarkAqua];
|
||||
} else {
|
||||
appearance = [NSAppearance appearanceNamed:NSAppearanceNameAqua];
|
||||
}
|
||||
|
||||
[NSApplication sharedApplication].appearance = appearance;
|
||||
}
|
||||
|
||||
/// True if the system is set to dark mode.
|
||||
BOOL system_is_dark(void) {
|
||||
BOOL styleSet = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleInterfaceStyle"] != nil;
|
||||
return styleSet;
|
||||
// FIXME: confirm whether this is required on 10.15/16 (it
|
||||
// does not appear to be on 11)
|
||||
|
||||
// BOOL autoSwitch = [[NSUserDefaults standardUserDefaults] boolForKey:@"AppleInterfaceStyleSwitchesAutomatically"];
|
||||
//
|
||||
// if (@available(macOS 10.15, *)) {
|
||||
// return autoSwitch ? !styleSet : styleSet;
|
||||
// } else {
|
||||
// return styleSet;
|
||||
// }
|
||||
|
||||
}
|
|
@ -1,20 +1,15 @@
|
|||
load("@rules_cc//cc:defs.bzl", "objc_library")
|
||||
|
||||
objc_library(
|
||||
name = "helper_static",
|
||||
srcs = ["AnkiHelper.m"],
|
||||
enable_modules = True,
|
||||
sdk_frameworks = ["AppKit"],
|
||||
py_binary(
|
||||
name = "helper_build",
|
||||
srcs = ["helper_build.py"],
|
||||
tags = ["manual"],
|
||||
alwayslink = True,
|
||||
)
|
||||
|
||||
apple_binary(
|
||||
genrule(
|
||||
name = "helper_dylib",
|
||||
binary_type = "dylib",
|
||||
minimum_os_version = "10.14",
|
||||
platform_type = "macos",
|
||||
srcs = glob(["*.swift"]),
|
||||
outs = ["libankihelper.dylib"],
|
||||
cmd = "$(location :helper_build) $@ $(COMPILATION_MODE) $(SRCS)",
|
||||
tags = ["manual"],
|
||||
tools = [":helper_build"],
|
||||
visibility = ["//qt:__subpackages__"],
|
||||
deps = ["helper_static"],
|
||||
)
|
||||
|
|
|
@ -1 +1 @@
|
|||
Tiny helper library to set dark mode on and off on Macs.
|
||||
Helper library for macOS-specific functionality.
|
||||
|
|
|
@ -7,11 +7,11 @@
|
|||
objects = {
|
||||
|
||||
/* Begin PBXBuildFile section */
|
||||
1327600A274613D9001D63D7 /* AnkiHelper.m in Sources */ = {isa = PBXBuildFile; fileRef = 13276009274613D8001D63D7 /* AnkiHelper.m */; };
|
||||
137892AC275D90FC009D0B6E /* theme.swift in Sources */ = {isa = PBXBuildFile; fileRef = 137892AB275D90FC009D0B6E /* theme.swift */; };
|
||||
/* End PBXBuildFile section */
|
||||
|
||||
/* Begin PBXFileReference section */
|
||||
13276009274613D8001D63D7 /* AnkiHelper.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AnkiHelper.m; sourceTree = "<group>"; };
|
||||
137892AB275D90FC009D0B6E /* theme.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = theme.swift; sourceTree = "<group>"; };
|
||||
138B770F2746137F003A3E4F /* libankihelper.dylib */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.dylib"; includeInIndex = 0; path = libankihelper.dylib; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
/* End PBXFileReference section */
|
||||
|
||||
|
@ -29,7 +29,7 @@
|
|||
138B77062746137F003A3E4F = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
13276009274613D8001D63D7 /* AnkiHelper.m */,
|
||||
137892AB275D90FC009D0B6E /* theme.swift */,
|
||||
138B77102746137F003A3E4F /* Products */,
|
||||
);
|
||||
sourceTree = "<group>";
|
||||
|
@ -83,6 +83,7 @@
|
|||
TargetAttributes = {
|
||||
138B770E2746137F003A3E4F = {
|
||||
CreatedOnToolsVersion = 13.1;
|
||||
LastSwiftMigration = 1310;
|
||||
};
|
||||
};
|
||||
};
|
||||
|
@ -109,7 +110,7 @@
|
|||
isa = PBXSourcesBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
1327600A274613D9001D63D7 /* AnkiHelper.m in Sources */,
|
||||
137892AC275D90FC009D0B6E /* theme.swift in Sources */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
|
@ -230,26 +231,41 @@
|
|||
138B77192746137F003A3E4F /* Debug */ = {
|
||||
isa = XCBuildConfiguration;
|
||||
buildSettings = {
|
||||
CLANG_ENABLE_MODULES = YES;
|
||||
CODE_SIGN_STYLE = Automatic;
|
||||
DEVELOPMENT_TEAM = 7ZM8SLJM4P;
|
||||
DYLIB_COMPATIBILITY_VERSION = 1;
|
||||
DYLIB_CURRENT_VERSION = 1;
|
||||
EXECUTABLE_PREFIX = lib;
|
||||
LD_RUNPATH_SEARCH_PATHS = (
|
||||
"$(inherited)",
|
||||
"@executable_path/../Frameworks",
|
||||
"@loader_path/../Frameworks",
|
||||
);
|
||||
PRODUCT_NAME = "$(TARGET_NAME)";
|
||||
SKIP_INSTALL = YES;
|
||||
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
|
||||
SWIFT_VERSION = 5.0;
|
||||
};
|
||||
name = Debug;
|
||||
};
|
||||
138B771A2746137F003A3E4F /* Release */ = {
|
||||
isa = XCBuildConfiguration;
|
||||
buildSettings = {
|
||||
CLANG_ENABLE_MODULES = YES;
|
||||
CODE_SIGN_STYLE = Automatic;
|
||||
DEVELOPMENT_TEAM = 7ZM8SLJM4P;
|
||||
DYLIB_COMPATIBILITY_VERSION = 1;
|
||||
DYLIB_CURRENT_VERSION = 1;
|
||||
EXECUTABLE_PREFIX = lib;
|
||||
LD_RUNPATH_SEARCH_PATHS = (
|
||||
"$(inherited)",
|
||||
"@executable_path/../Frameworks",
|
||||
"@loader_path/../Frameworks",
|
||||
);
|
||||
PRODUCT_NAME = "$(TARGET_NAME)";
|
||||
SKIP_INSTALL = YES;
|
||||
SWIFT_VERSION = 5.0;
|
||||
};
|
||||
name = Release;
|
||||
};
|
||||
|
|
Binary file not shown.
|
@ -0,0 +1,5 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict />
|
||||
</plist>
|
|
@ -4,7 +4,7 @@
|
|||
<dict>
|
||||
<key>SchemeUserState</key>
|
||||
<dict>
|
||||
<key>ankihelper.xcscheme_^#shared#^_</key>
|
||||
<key>ankihelper.xcscheme</key>
|
||||
<dict>
|
||||
<key>orderHint</key>
|
||||
<integer>0</integer>
|
||||
|
|
29
qt/mac/helper_build.py
Normal file
29
qt/mac/helper_build.py
Normal file
|
@ -0,0 +1,29 @@
|
|||
# Copyright: Ankitects Pty Ltd and contributors
|
||||
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
||||
|
||||
import subprocess
|
||||
import sys
|
||||
import platform
|
||||
from pathlib import Path
|
||||
|
||||
out_dylib, compile_mode, *src_files = sys.argv[1:]
|
||||
out_dir = Path(out_dylib).parent.resolve()
|
||||
src_dir = Path(src_files[0]).parent.resolve()
|
||||
|
||||
if platform.machine() == "arm64":
|
||||
target = "arm64-apple-macos11"
|
||||
else:
|
||||
target = "x86_64-apple-macos10.14"
|
||||
|
||||
args = [
|
||||
"swiftc",
|
||||
"-target",
|
||||
target,
|
||||
"-emit-library",
|
||||
"-module-name",
|
||||
"ankihelper",
|
||||
]
|
||||
if compile_mode == "opt":
|
||||
args.append("-O")
|
||||
args.extend(src_dir / Path(file).name for file in src_files)
|
||||
subprocess.run(args, check=True, cwd=out_dir)
|
18
qt/mac/theme.swift
Normal file
18
qt/mac/theme.swift
Normal file
|
@ -0,0 +1,18 @@
|
|||
// Copyright: Ankitects Pty Ltd and contributors
|
||||
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
||||
|
||||
import AppKit
|
||||
import Foundation
|
||||
|
||||
/// Force our app to be either light or dark mode.
|
||||
@_cdecl("set_darkmode_enabled")
|
||||
public func setDarkmodeEnabled(_ enabled: Bool) {
|
||||
NSApplication.shared.appearance = NSAppearance(named: enabled ? .darkAqua : .aqua)
|
||||
}
|
||||
|
||||
/// True if the system is set to dark mode.
|
||||
@_cdecl("system_is_dark")
|
||||
public func systemIsDark() -> Bool {
|
||||
let styleSet = UserDefaults.standard.object(forKey: "AppleInterfaceStyle") != nil
|
||||
return styleSet
|
||||
}
|
Loading…
Reference in a new issue