Winstoon thanks winstoon,I don't familiar with AppKit, can you help me fix the code, Thanks. below is my code:
export function toggleToolbar(context) {
const threadDictionary = NSThread.mainThread().threadDictionary()
if (threadDictionary[SidePanelIdentifier]) {
onShutdown(context)
} else {
showToolbar()
}
}
export function showToolbar() {
observerWindowResizeNotification(() => {
const curWebView = BrowserManage.getCurrent()
if (curWebView) {
curWebView.updatePosition()
}
})
const toolbar = NSStackView.alloc().initWithFrame(NSMakeRect(0, 0, 40, 400))
const threadDictionary = NSThread.mainThread().threadDictionary()
threadDictionary[SidePanelIdentifier] = toolbar
toolbar.identifier = SidePanelIdentifier
toolbar.setSpacing(8)
toolbar.setFlipped(true)
toolbar.setBackgroundColor(NSColor.windowBackgroundColor())
toolbar.orientation = 1
toolbar.addView_inGravity(createImageView(NSMakeRect(0, 0, 40, 22), 'transparent', NSMakeSize(40, 22)), 1)
const Logo = createImageView(NSMakeRect(0, 0, 40, 30), 'logo', NSMakeSize(24, 17))
toolbar.addSubview(Logo)
Menus.forEach((item, index) => {
const {
rect = NSMakeRect(0, 0, 40, 40),
size = NSMakeSize(16, 16),
icon,
activeIcon,
disabledIcon,
tooltip,
identifier,
type = 2,
inGravityType = 1,
disabled,
webviewConfig
} = item
const displayIcon = isDarkMode ? (disabled ? icon : disabledIcon) : (disabled ? disabledIcon : icon)
const Button = addButton({
rect,
size,
icon: displayIcon,
activeIcon: disabled ? disabledIcon : activeIcon,
tooltip,
identifier,
type,
callbackAction: sender => {
if (disabled) return
checkVersion()
const threadDictionary = NSThread.mainThread().threadDictionary()
const menuBtnRegExp = new RegExp(`${IdentifierPrefix}-menu.*`)
for (const k in threadDictionary) {
if (menuBtnRegExp.test(k) && k !== identifier) {
threadDictionary[k].setState(NSOffState)
}
}
}
}
})
threadDictionary[identifier] = Button
toolbar.addView_inGravity(createBoxSeparator(), inGravityType)
toolbar.addView_inGravity(Button, inGravityType)
if (index === Menus.length - 1) {
toolbar.addView_inGravity(createBoxSeparator(), 1)
}
})
insertToolbar(toolbar, SidePanelIdentifier)
}
export function insertToolbar(toolbar, identifier) {
const contentView = document.documentWindow().contentView()
const stageView = contentView.subviews().objectAtIndex(0)
const views = stageView.subviews()
const exist = helper.toArray(views).some(d => helper.toJSString(d.identifier()) === identifier)
if (exist) {
return
}
COScript.currentCOScript().setShouldKeepAround(true)
const finalViews = []
let pushedWebView = false
for (let i = 0; i < views.count(); i++) {
const view = views[i]
finalViews.push(view)
if (!pushedWebView && helper.toJSString(view.identifier()) === 'view_canvas') {
finalViews.push(toolbar)
setSettingForKey(SidePanelIdentifier, 'true')
pushedWebView = true
}
}
stageView.subviews = finalViews
stageView.adjustSubviews()
}
export function removeToolbar(identifier) {
const contentView = document.documentWindow().contentView()
if (!contentView) {
return false
}
const stageView = contentView.subviews().objectAtIndex(0)
const finalViews = helper.toArray(stageView.subviews()).filter(view => {
return helper.toJSString(view.identifier()) !== identifier
})
removeSettingForKey(SidePanelIdentifier)
stageView.subviews = finalViews
stageView.adjustSubviews()
}