Skip to content

Instantly share code, notes, and snippets.

View boraseoksoon's full-sized avatar
:octocat:
HQL, HLVM

장석순(Jang Seoksoon) boraseoksoon

:octocat:
HQL, HLVM
View GitHub Profile
// openSafari.js;
/**
* Function to open Safari browser on macOS using AppleScript
*/
async function openSafari() {
// Create the AppleScript command to activate Safari
const command = `osascript -e 'tell application "Safari" to activate'`;
// Run the command via bash in a subprocess
@boraseoksoon
boraseoksoon / highlightView.swift
Created September 1, 2023 00:18
HighlightView
//
// HighlightView.swift
// DemoScreen
//
// Created by seoksoon jang on 2023-08-31.
//
import SwiftUI
enum Light: Hashable, CaseIterable {
@boraseoksoon
boraseoksoon / syntax.js
Last active August 18, 2023 07:37
javascript syntax
`for
loop
javascript
`
`for
loop
javascript2
`
@boraseoksoon
boraseoksoon / advance_throttle.swift
Last active June 13, 2023 20:08
Throttle function to be able to guarantee first and last operations.
enum TaskType {
case current
case main
}
enum ThrottleType {
case none
case immediateFirst
case guaranteeLast
case both
@boraseoksoon
boraseoksoon / whenHovered.md
Created May 28, 2023 04:20 — forked from importRyan/whenHovered.md
Reliable SwiftUI mouse hover

Reliable mouseEnter/Exit for SwiftUI

Kapture 2021-03-01 at 14 43 39

On Mac, SwiftUI's .onHover closure is not always called on mouse exit, particularly with high cursor velocity. A grid of targets or with finer target shapes will often have multiple targets falsely active after the mouse has moved on.

It is easy to run back to AppKit's safety. Below is a SwiftUI-like modifier for reliable mouse-tracking. You can easily adapt it for other mouse tracking needs.

import SwiftUI
@boraseoksoon
boraseoksoon / debounce.swift
Created May 10, 2023 07:33
Swift debounce using DispatchWorkItem
func debounce(delay: TimeInterval, action: @escaping () -> Void) -> () -> Void {
var currentWorkItem: DispatchWorkItem?
return {
currentWorkItem?.cancel()
currentWorkItem = DispatchWorkItem(block: action)
DispatchQueue.main.asyncAfter(deadline: .now() + delay, execute: currentWorkItem!)
}
}
@boraseoksoon
boraseoksoon / safe.swift
Created May 9, 2023 02:23
Swift safe access to index of collection
extension Collection where Indices.Iterator.Element == Index {
subscript (safe index: Index) -> Iterator.Element? {
return indices.contains(index) ? self[index] : nil
}
}
// let array = [0,1,2,3,4,5]
// array[safe: 10]
@boraseoksoon
boraseoksoon / json.swift
Created May 8, 2023 21:43
Swift json encode/decode using NSJSONSerialization
import Foundation
extension JSONConvertible {
func toJSONString() -> String? {
return (try? JSONSerialization.data(withJSONObject: toDictionary(), options: []))
.flatMap { String(data: $0, encoding: .utf8) }
}
static func fromJSONString(_ jsonString: String) -> Self? {
return (try? JSONSerialization.jsonObject(with: Data(jsonString.utf8), options: []))
@boraseoksoon
boraseoksoon / SpotlightAppSearch.swift
Created May 7, 2023 00:55
Spotlight App Search minimal UI + functionality
//
// ContentView.swift
// SpotlightAppSearch
//
// Created by seoksoon jang on 2023-05-06.
//
import SwiftUI
import CoreServices
@boraseoksoon
boraseoksoon / MonacoWebView.swift
Created March 5, 2023 08:40
Load Monaco editor in a single html file for MacOS SwiftUI
import SwiftUI
import WebKit
@main
struct MonacoApp: App {
let codeSnippet = """
var body: some Scene {
WindowGroup {
EditorView(content: codeSnippet, language: "swift", theme: "vs-dark")
}