echoes 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'objc'
4
+
5
+ module Echoes
6
+ # Thin wrapper around `[NSUserDefaults initWithSuiteName:]` for
7
+ # cross-launch persistence of small user-tweaked values (font size,
8
+ # …). The on-disk plist lives at `~/Library/Preferences/<SUITE>.plist`
9
+ # and `defaults read|write|delete <SUITE>` from the shell works as
10
+ # expected.
11
+ #
12
+ # We use an explicit suite name rather than `standardUserDefaults`
13
+ # because echoes runs under `bundle exec exe/echoes` (no Info.plist
14
+ # bundle ID) and as a future `.app`. A fixed suite gives stable
15
+ # storage across both.
16
+ module Preferences
17
+ SUITE = 'jp.dio.echoes'
18
+
19
+ def self.defaults
20
+ @defaults ||= begin
21
+ obj = ObjC::MSG_PTR.call(ObjC.cls('NSUserDefaults'), ObjC.sel('alloc'))
22
+ ObjC::MSG_PTR_1.call(obj, ObjC.sel('initWithSuiteName:'), ObjC.nsstring(SUITE))
23
+ end
24
+ end
25
+
26
+ # Returns the stored Float for `key`, or `default` if the key isn't
27
+ # set. We round-trip through `objectForKey:` so we can distinguish
28
+ # "missing" from "set to 0.0" — `doubleForKey:` collapses both.
29
+ def self.fetch_double(key, default:)
30
+ obj = ObjC::MSG_PTR_1.call(defaults, ObjC.sel('objectForKey:'), ObjC.nsstring(key.to_s))
31
+ return default if obj.null?
32
+ ObjC::MSG_RET_D.call(obj, ObjC.sel('doubleValue'))
33
+ end
34
+
35
+ def self.set_double(key, value)
36
+ ObjC::MSG_VOID_D_1.call(defaults, ObjC.sel('setDouble:forKey:'),
37
+ value.to_f, ObjC.nsstring(key.to_s))
38
+ end
39
+
40
+ def self.delete(key)
41
+ ObjC::MSG_VOID_1.call(defaults, ObjC.sel('removeObjectForKey:'),
42
+ ObjC.nsstring(key.to_s))
43
+ end
44
+ end
45
+ end