Skip to content

Commit

Permalink
Fix for Sonoma
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexPerathoner committed Sep 28, 2023
1 parent 5943538 commit 682cdf8
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 55 deletions.
22 changes: 20 additions & 2 deletions Sessions Extension/SafariExtensionHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,28 @@ import os.log

class SafariExtensionHandler: SFSafariExtensionHandler {

override func beginRequest(with context: NSExtensionContext) {
let request = context.inputItems.first as? NSExtensionItem

let profile: UUID?
if #available(iOS 17.0, macOS 14.0, *) {
profile = request?.userInfo?[SFExtensionProfileKey] as? UUID
} else {
profile = request?.userInfo?["profile"] as? UUID
}

os_log(.default, "The extension received a request for profile: %@", profile?.uuidString ?? "none")
}

override func messageReceived(withName messageName: String, from page: SFSafariPage, userInfo: [String : Any]?) {
page.getPropertiesWithCompletionHandler { properties in
os_log(.default, "The extension received a message (%@) from a script injected into (%@) with userInfo (%@)", messageName, String(describing: properties?.url), userInfo ?? [:])
}
}

override func toolbarItemClicked(in window: SFSafariWindow) {
// This method will be called when your toolbar item is clicked.
NSLog("The extension's toolbar item was clicked")
os_log(.debug, "The extension's toolbar item was clicked")
}

override func validateToolbarItem(in window: SFSafariWindow, validationHandler: @escaping ((Bool, String) -> Void)) {
Expand All @@ -24,7 +42,7 @@ class SafariExtensionHandler: SFSafariExtensionHandler {
}

override func popoverViewController() -> SFSafariExtensionViewController {
os_log(.error, "showing popup")
os_log(.debug, "Showing popup")
return SafariExtensionViewController.shared
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="17132" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="22154" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<dependencies>
<deployment identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="17132"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="22154"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
Expand All @@ -23,7 +23,7 @@
<rect key="frame" x="0.0" y="0.0" width="243" height="321"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<subviews>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="eGs-lU-tPl">
<textField focusRingType="none" horizontalHuggingPriority="251" verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="eGs-lU-tPl">
<rect key="frame" x="18" y="278" width="134" height="16"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<textFieldCell key="cell" lineBreakMode="clipping" title="Save current session:" id="Ut8-17-UtM">
Expand All @@ -46,7 +46,7 @@
<color key="backgroundColor" name="controlBackgroundColor" catalog="System" colorSpace="catalog"/>
<color key="gridColor" name="gridColor" catalog="System" colorSpace="catalog"/>
<tableColumns>
<tableColumn identifier="titleColumn" width="154" minWidth="40" maxWidth="1000" id="FFK-dH-gPd">
<tableColumn identifier="titleColumn" width="136" minWidth="40" maxWidth="1000" id="FFK-dH-gPd">
<tableHeaderCell key="headerCell" lineBreakMode="truncatingTail" borderStyle="border" title="title">
<color key="textColor" name="headerTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="headerColor" catalog="System" colorSpace="catalog"/>
Expand All @@ -59,11 +59,11 @@
<tableColumnResizingMask key="resizingMask" resizeWithTable="YES" userResizable="YES"/>
<prototypeCellViews>
<tableCellView identifier="titleCell" id="7Y2-M6-ubJ">
<rect key="frame" x="1" y="0.0" width="154" height="17"/>
<rect key="frame" x="1" y="0.0" width="141" height="17"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" horizontalCompressionResistancePriority="250" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="HAl-sE-HA2">
<rect key="frame" x="0.0" y="1" width="154" height="16"/>
<textField focusRingType="none" horizontalHuggingPriority="251" verticalHuggingPriority="750" horizontalCompressionResistancePriority="250" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="HAl-sE-HA2">
<rect key="frame" x="0.0" y="1" width="141" height="16"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMinY="YES" flexibleMaxY="YES"/>
<textFieldCell key="cell" lineBreakMode="truncatingTail" selectable="YES" editable="YES" sendsActionOnEndEditing="YES" title="Table View Cell" id="g7X-L4-u32">
<font key="font" usesAppearanceFont="YES"/>
Expand All @@ -78,7 +78,7 @@
</tableCellView>
</prototypeCellViews>
</tableColumn>
<tableColumn identifier="nameColumn" width="41" minWidth="40" maxWidth="1000" id="e7Y-MQ-FFZ">
<tableColumn identifier="nameColumn" width="50" minWidth="40" maxWidth="1000" id="e7Y-MQ-FFZ">
<tableHeaderCell key="headerCell" lineBreakMode="truncatingTail" borderStyle="border" title="status">
<color key="textColor" name="headerTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="headerColor" catalog="System" colorSpace="catalog"/>
Expand All @@ -87,7 +87,7 @@
<tableColumnResizingMask key="resizingMask" resizeWithTable="YES" userResizable="YES"/>
<prototypeCellViews>
<imageView identifier="imageCell" horizontalHuggingPriority="251" verticalHuggingPriority="251" id="2z7-2k-Tmx">
<rect key="frame" x="158" y="0.0" width="41" height="22"/>
<rect key="frame" x="145" y="0.0" width="54" height="22"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<imageCell key="cell" refusesFirstResponder="YES" alignment="left" imageScaling="proportionallyDown" image="NSStatusPartiallyAvailable" id="Oro-Ik-CNg"/>
</imageView>
Expand All @@ -111,7 +111,7 @@
<autoresizingMask key="autoresizingMask"/>
</scroller>
</scrollView>
<searchField wantsLayer="YES" verticalHuggingPriority="750" fixedFrame="YES" textCompletion="NO" translatesAutoresizingMaskIntoConstraints="NO" id="DvB-ZS-o0O">
<searchField wantsLayer="YES" focusRingType="none" verticalHuggingPriority="750" fixedFrame="YES" textCompletion="NO" translatesAutoresizingMaskIntoConstraints="NO" id="DvB-ZS-o0O">
<rect key="frame" x="20" y="215" width="203" height="22"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<searchFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" selectable="YES" editable="YES" continuous="YES" borderStyle="bezel" usesSingleLineMode="YES" bezelStyle="round" sendsSearchStringImmediately="YES" id="uB0-Sl-tmZ">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@

import SafariServices
import AppKit
import os.log

class SafariExtensionViewController: SFSafariExtensionViewController, NSControlTextEditingDelegate {

static let shared = SafariExtensionViewController()
var sessions = [Session]() {
didSet {
print("Value changed - Now \(sessions.count) sessions")
os_log(.debug, "Value changed - Now %d sessions", sessions.count)
saveSessions(session: sessions) //saves to userdefaults
tableView.reloadData()
}
Expand Down Expand Up @@ -43,7 +44,7 @@ class SafariExtensionViewController: SFSafariExtensionViewController, NSControlT
setupSearchfield()
preferredContentSize = NSSize(width: 243, height: 321)
if let r: [Session] = retrieveSession() {
print("Sessions retrieved")
os_log(.info, "Sessions retrieved")
sessions = r
}
ignoringPinnedTabsOutlet.state =
Expand Down Expand Up @@ -76,23 +77,27 @@ class SafariExtensionViewController: SFSafariExtensionViewController, NSControlT
// MARK: - IBActions

@IBAction func addSession(_ sender: Any) {
SFSafariApplication.getActiveWindow { (window) in
self.getTabs(window: window) { (actTabs) in
if actTabs.count > 0 {

let name = actTabs.first!.title
let id = self.generateId()

//adds to the top of the table (note insert at)
self.sessions.insert(Session(name: name, pages: actTabs, id: id), at: 0)

//adds to the bottom of the table
//self.sessions.append(Session(name: name, pages: actTabs))

print("Added session")
}
}
}
os_log(.debug, "Adding session...")
DispatchQueue.global().sync {
SFSafariApplication.getActiveWindow() { (window) in
os_log(.debug, "retrieved active window")
self.getTabs(window: window) { (actTabs) in
if actTabs.count > 0 {

let name = actTabs.first!.title
let id = self.generateId()

//adds to the top of the table (note insert at)
self.sessions.insert(Session(name: name, pages: actTabs, id: id), at: 0)

//adds to the bottom of the table
//self.sessions.append(Session(name: name, pages: actTabs))

os_log(.debug, "Added session.")
}
}
}
}
}


Expand All @@ -103,12 +108,12 @@ class SafariExtensionViewController: SFSafariExtensionViewController, NSControlT
let index = tableView.row(for: sender)
sessions[index].name = newName
saveSessions(session: sessions)
print("Name changed to \(newName)")
os_log(.debug, "Name changed to %@", newName)
}


@IBAction func restoreMenuItem(sender: Any) {
NSLog("Restore menu item...")
os_log(.debug, "Restore menu item...")

if(UserDefaults.standard.bool(forKey: Constants.alwaysAutoUpdate)) {
restoreAndAutoUpdateClicked(sender)
Expand Down Expand Up @@ -146,10 +151,10 @@ class SafariExtensionViewController: SFSafariExtensionViewController, NSControlT
SFSafariApplication.getActiveWindow { (window) in
self.getTabs(window: window) { (actTabs) in
if actTabs.count > 0 {
NSLog("Replacing session \(clickedRow) with a session of \(actTabs.count) tabs")
os_log(.debug, "Replacing session @s with a session of %d tabs", clickedRow, actTabs.count)
self.replacePagesInSession(index: clickedRow, pages: actTabs)
} else {
NSLog("ERROR: No tabs found while trying to replace")
os_log(.error, "No tabs found while trying to replace")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@

import Cocoa
import SafariServices
import os.log

extension SafariExtensionViewController {
// MARK: - Userdefaults

func retrieveSession() -> [Session]? {
NSLog("Try to retrieve session...")
os_log(.info, "Try to retrieve session...")
if let unarchivedObject = UserDefaults.standard.object(forKey: "pages") as? NSData {
do {
return try (NSKeyedUnarchiver.unarchivedObject(ofClasses: [Session.self, NSArray.self, WebPage.self, NSURL.self], from: unarchivedObject as Data) as? [Session])
return try (NSKeyedUnarchiver.unarchivedObject(ofClasses: [Session.self, NSArray.self, WebPage.self, NSURL.self, NSString.self], from: unarchivedObject as Data) as? [Session])
} catch {
NSLog("Error!")
print("Error: \(error)")
os_log(.error, "Error while unarchiving sessions")
}
}
NSLog("Returning nil...")
os_log(.info, "No session found. Returning nil...")
return nil
}

Expand All @@ -32,7 +32,7 @@ extension SafariExtensionViewController {
let archivedObject = try NSKeyedArchiver.archivedData(withRootObject: sessions, requiringSecureCoding: false)
return archivedObject as NSData
} catch let error {
print("Unspecified Error: \(error)")
os_log(.error, "Unspecified Error: %@", error.localizedDescription)
}
return nil
}
Expand Down
Loading

0 comments on commit 682cdf8

Please sign in to comment.