Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
F
Front-End
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
abdullh.alsoleman
Front-End
Commits
99146c20
Unverified
Commit
99146c20
authored
Mar 28, 2023
by
Chris Bracken
Committed by
GitHub
Mar 28, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[macOS] platform_channel sample style cleanups (#123569)
[macOS] platform_channel sample style cleanups
parent
b0ad7c45
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
45 additions
and
36 deletions
+45
-36
MainFlutterWindow.swift
...les/platform_channel/macos/Runner/MainFlutterWindow.swift
+33
-28
PowerSource.swift
examples/platform_channel/macos/Runner/PowerSource.swift
+12
-8
No files found.
examples/platform_channel/macos/Runner/MainFlutterWindow.swift
View file @
99146c20
...
...
@@ -5,15 +5,16 @@
import
Cocoa
import
FlutterMacOS
class
MainFlutterWindow
:
NSWindow
,
FlutterStreamHandler
,
PowerSourceStateChangeDelegate
{
class
MainFlutterWindow
:
NSWindow
{
private
let
powerSource
=
PowerSource
()
private
let
stateChange
Source
=
PowerSourceStateChangeHandler
()
private
let
stateChange
Handler
=
PowerSourceStateChangeHandler
()
private
var
eventSink
:
FlutterEventSink
?
override
func
awakeFromNib
()
{
let
flutterViewController
=
FlutterViewController
()
let
windowFrame
=
self
.
frame
self
.
contentViewController
=
flutterViewController
self
.
displayIfNeeded
()
self
.
setFrame
(
windowFrame
,
display
:
true
)
// Register battery method channel.
...
...
@@ -21,18 +22,18 @@ class MainFlutterWindow: NSWindow, FlutterStreamHandler, PowerSourceStateChangeD
let
batteryChannel
=
FlutterMethodChannel
(
name
:
"samples.flutter.io/battery"
,
binaryMessenger
:
registrar
.
messenger
)
batteryChannel
.
setMethodCallHandler
({
[
weak
self
]
(
call
,
result
)
in
batteryChannel
.
setMethodCallHandler
{
[
powerSource
=
self
.
powerSource
]
(
call
,
result
)
in
switch
call
.
method
{
case
"getBatteryLevel"
:
if
self
?
.
powerSource
.
hasBattery
()
==
false
{
result
(
FlutterError
(
code
:
"NO_BATTERY"
,
message
:
"Device does not have a battery"
,
details
:
nil
))
guard
powerSource
.
hasBattery
()
else
{
result
(
FlutterError
(
code
:
"NO_BATTERY"
,
message
:
"Device does not have a battery"
,
details
:
nil
))
return
}
let
level
=
self
?
.
powerSource
.
getCurrentCapacity
()
if
level
==
-
1
{
guard
let
level
=
powerSource
.
getCurrentCapacity
()
else
{
result
(
FlutterError
(
code
:
"UNAVAILABLE"
,
...
...
@@ -44,7 +45,7 @@ class MainFlutterWindow: NSWindow, FlutterStreamHandler, PowerSourceStateChangeD
default
:
result
(
FlutterMethodNotImplemented
)
}
}
)
}
// Register charging event channel.
let
chargingChannel
=
FlutterEventChannel
(
...
...
@@ -57,36 +58,40 @@ class MainFlutterWindow: NSWindow, FlutterStreamHandler, PowerSourceStateChangeD
super
.
awakeFromNib
()
}
/// Emit a power status event to the registered event sink.
func
emitPowerStatusEvent
()
{
if
let
sink
=
self
.
eventSink
{
switch
self
.
powerSource
.
getPowerState
()
{
case
.
ac
:
sink
(
"charging"
)
case
.
battery
:
sink
(
"discharging"
)
case
.
unknown
:
sink
(
"UNAVAILABLE"
)
}
}
}
}
extension
MainFlutterWindow
:
FlutterStreamHandler
{
func
onListen
(
withArguments
arguments
:
Any
?,
eventSink
events
:
@escaping
FlutterEventSink
)
->
FlutterError
?
{
self
.
eventSink
=
events
self
.
emitPowerStatusEvent
()
self
.
stateChange
Source
.
delegate
=
self
self
.
stateChange
Handler
.
delegate
=
self
return
nil
}
func
onCancel
(
withArguments
arguments
:
Any
?)
->
FlutterError
?
{
self
.
stateChange
Source
.
delegate
=
nil
self
.
stateChange
Handler
.
delegate
=
nil
self
.
eventSink
=
nil
return
nil
}
}
func
onPowerSourceStateChanged
()
{
extension
MainFlutterWindow
:
PowerSourceStateChangeDelegate
{
func
didChangePowerSourceState
()
{
self
.
emitPowerStatusEvent
()
}
func
emitPowerStatusEvent
()
{
if
let
sink
=
self
.
eventSink
{
switch
self
.
powerSource
.
getPowerState
()
{
case
.
ac
:
sink
(
"charging"
)
case
.
battery
:
sink
(
"discharging"
)
case
.
unknown
:
sink
(
"UNAVAILABLE"
)
}
}
}
}
examples/platform_channel/macos/Runner/PowerSource.swift
View file @
99146c20
...
...
@@ -12,9 +12,13 @@ enum PowerState {
}
/// A convenience wrapper for an IOKit power source.
class
PowerSource
{
final
class
PowerSource
{
let
info
=
IOPSCopyPowerSourcesInfo
()
.
takeRetainedValue
()
lazy
var
sources
=
IOPSCopyPowerSourcesList
(
info
)
.
takeRetainedValue
()
as
Array
let
sources
:
Array
<
CFTypeRef
>
init
()
{
sources
=
IOPSCopyPowerSourcesList
(
info
)
.
takeRetainedValue
()
as
Array
}
func
hasBattery
()
->
Bool
{
return
!
sources
.
isEmpty
...
...
@@ -22,7 +26,7 @@ class PowerSource {
/// Returns the current power source capacity. Apple-defined power sources will return this value
/// as a percentage.
func
getCurrentCapacity
()
->
Int
{
func
getCurrentCapacity
()
->
Int
?
{
if
let
source
=
sources
.
first
{
let
description
=
IOPSGetPowerSourceDescription
(
info
,
source
)
.
takeUnretainedValue
()
as!
[
String
:
AnyObject
]
...
...
@@ -30,7 +34,7 @@ class PowerSource {
return
level
}
}
return
-
1
return
nil
}
/// Returns whether the device is drawing battery power or connected to an external power source.
...
...
@@ -54,11 +58,11 @@ class PowerSource {
}
protocol
PowerSourceStateChangeDelegate
:
AnyObject
{
func
onPowerSourceStateChanged
()
func
didChangePowerSourceState
()
}
/// A listener for system power source state change events. Notifies the delegate on each event.
class
PowerSourceStateChangeHandler
{
final
class
PowerSourceStateChangeHandler
{
private
var
runLoopSource
:
CFRunLoopSource
?
weak
var
delegate
:
PowerSourceStateChangeDelegate
?
...
...
@@ -66,10 +70,10 @@ class PowerSourceStateChangeHandler {
let
context
=
UnsafeMutableRawPointer
(
Unmanaged
.
passUnretained
(
self
)
.
toOpaque
())
self
.
runLoopSource
=
IOPSNotificationCreateRunLoopSource
(
{
(
context
:
UnsafeMutableRawPointer
?)
in
let
weak
Self
=
Unmanaged
<
PowerSourceStateChangeHandler
>.
fromOpaque
(
let
unowned
Self
=
Unmanaged
<
PowerSourceStateChangeHandler
>.
fromOpaque
(
UnsafeRawPointer
(
context
!
)
)
.
takeUnretainedValue
()
weakSelf
.
delegate
?
.
onPowerSourceStateChanged
()
unownedSelf
.
delegate
?
.
didChangePowerSourceState
()
},
context
)
.
takeRetainedValue
()
CFRunLoopAddSource
(
CFRunLoopGetCurrent
(),
self
.
runLoopSource
,
.
defaultMode
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment