Skip to content

Commit

Permalink
fix(addon): all issues about studyController frame
Browse files Browse the repository at this point in the history
  • Loading branch information
ourongxing committed May 17, 2024
1 parent c30f96d commit d40fc2b
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 28 deletions.
26 changes: 21 additions & 5 deletions packages/ohmymn/src/jsExtension/handleReceivedEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { actionBarController } from "~/modules/toolbar/utils"
function isDocSide(x: number) {
const { studyController } = MN
const { width: readerViewWidth } = studyController.readerController.view.frame
const { width } = studyController.view.bounds
const { width } = studyController.view.frame
if (
studyController.docMapSplitMode == DocMapSplitMode.half &&
((studyController.rightMapMode && x < readerViewWidth) ||
Expand Down Expand Up @@ -113,8 +113,14 @@ export default defineEventHandlers<
const [x, y, width, height] = JSON.parse(
`[${sender.userInfo.winRect.replace(/[{}]/g, "")}]`
) as number[]
const frame = MN.studyController.view.frame
self.bar.text = {
winRect: { x, y, width, height },
winRect: {
x: x - frame.x,
y: y - frame.y,
width,
height
},
arrow: sender.userInfo.arrow
}
actionBarController("text")?.add()
Expand All @@ -132,17 +138,27 @@ export default defineEventHandlers<
},
async onPopupMenuOnNote(sender) {
if (self.window !== MN.currentWindow) return
// MN.log(MN.studyController.view.frame)
// MN.log(MN.studyController.view.bounds)
// alert(JSON.stringify(MN.studyController.view.frame))
if (MN.studyController.studyMode !== StudyMode.study) return
MN.log("Popup menu on note open", "event")
if (isModuleON("gesture") || isModuleON("toolbar")) {
/**
* studyController.view 的 frame 和 bounds 的区别,frame 是相对于父视图,bounds 是相对于自身。宽度和高度始终保持一致。
* frame 的 x 和 y 是有值的,而且 y 一直是 30 (MN3 上是 20,在 iPad 上又都是 24),x 在打开侧边栏的情况下会变为 280。
* bounds 的 x 和 y 一直是 0。
* 影响最严重的就是 winRect 了,导致后面的 Gesture 以及 Toolbar 适配困难。
*/
const frame = MN.studyController.view.frame
const [x, y, width, height] = JSON.parse(
`[${sender.userInfo.winRect.replace(/[{}]/g, "")}]`
)
if (isDocSide(x + width)) return
if (isDocSide(x - frame.x + width)) return
self.bar.card = {
winRect: {
x,
y,
x: x - frame.x,
y: y - frame.y,
width,
height
},
Expand Down
3 changes: 2 additions & 1 deletion packages/ohmymn/src/jsExtension/lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ export default defineLifecycleHandlers({
status: false,
lastOpenPanel: 0,
lastClickButton: 0,
lastReaderViewWidth: 0
lastReaderViewWidth: 0,
lastStudyControllerViewWidth: 0
}
self.metadata = {
data: undefined,
Expand Down
12 changes: 8 additions & 4 deletions packages/ohmymn/src/jsExtension/switchPanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ export function layoutViewController(
const { studyController } = MN
const readerView = studyController.readerController.view
self.panel.lastReaderViewWidth = readerView.frame.width
const frame = studyController.view.bounds
self.panel.lastStudyControllerViewWidth = studyController.view.frame.width
const frame = studyController.view.frame
const width = 300
const customFrame = JSON.parse(self.globalProfile.additional.settingViewFrame)
const height = [600, 450, 300, customFrame.height ?? 450][heightNum]
Expand Down Expand Up @@ -136,10 +137,13 @@ export function switchPanel(sender?: UIView) {
function controllerWillLayoutSubviews(controller: UIViewController) {
if (controller != MN.studyController) return
if (!self.panel.status) return
layoutViewController()
if (
Date.now() - self.panel.lastOpenPanel < 200 ||
(self.panel.lastReaderViewWidth !==
MN.studyController.readerController.view.frame.width &&
((self.panel.lastReaderViewWidth !==
MN.studyController.readerController.view.frame.width ||
self.panel.lastStudyControllerViewWidth !==
MN.studyController.view.frame.width) &&
[0, 1, 2].includes(self.globalProfile.addon.panelPosition[0]))
) {
layoutViewController()
Expand Down Expand Up @@ -190,7 +194,7 @@ function onCloseButtonClick() {

export function ensureSafety(
{ x, y }: CGPoint,
frame = MN.studyController.view.bounds,
frame = MN.studyController.view.frame,
dragOverlayFrame = self.dragOverlayView.frame
) {
let tmp = x
Expand Down
6 changes: 3 additions & 3 deletions packages/ohmymn/src/modules/gesture/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export function isWithinArea(
export function checkSwipePosition(sender: UIGestureRecognizer): SwipePosition {
const { studyController } = MN
const { x: swipeX, y: swipeY } = sender.locationInView(studyController.view)
const { width, height } = studyController.view.bounds
const { width, height } = studyController.view.frame
// Block some UI that can be touched by mistake
if (swipeY < 100 || swipeX < 70 || swipeX > width - 70)
return SwipePosition.None
Expand All @@ -78,7 +78,7 @@ export function checkSwipePosition(sender: UIGestureRecognizer): SwipePosition {
isWithinArea(
{ swipeY },
{
y: (arrow === DirectionOfSelection.toLeft ? y - 67 : y + 1) - 3,
y: arrow === DirectionOfSelection.toLeft ? y - 47 : y + 21,
height: 40
}
)
Expand Down Expand Up @@ -112,7 +112,7 @@ export function checkSwipePosition(sender: UIGestureRecognizer): SwipePosition {
isWithinArea(
{ swipeY },
{
y: (cardY > 60 ? cardY - 67 : cardY + cardHeight - 9) - 3,
y: cardY > 40 ? cardY - 47 : cardY + cardHeight + 11,
height: 40
}
)
Expand Down
20 changes: 5 additions & 15 deletions packages/ohmymn/src/modules/toolbar/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,7 @@ export function actionBarController(type: "card" | "text") {
const { height, width: lastWidth } = self.cardActionBar.view.frame
self.cardActionBar.view.frame = {
x: winRect.x,
// y:
// (winRect.y > 60 ? winRect.y - 67 : winRect.y + winRect.height - 9) +
// (MN.isMac ? 0 : -3),
y:
(winRect.y > 60
? winRect.y - 67 - 35
: winRect.y + winRect.height - 9 + 40) + (MN.isMac ? 0 : -3),
y: winRect.y > 40 ? winRect.y - 82 : winRect.y + winRect.height + 51,
height,
width: winRect.width
}
Expand All @@ -35,14 +29,10 @@ export function actionBarController(type: "card" | "text") {
const { height } = self.textActionBar.view.frame
self.textActionBar.view.frame = {
x: winRect.x,
// y:
// (arrow === DirectionOfSelection.toLeft
// ? winRect.y - 67
// : winRect.y + 1) + (MN.isMac ? 0 : -3),
y:
(arrow === DirectionOfSelection.toLeft
? winRect.y - 67 - 35
: winRect.y + 1 + 40) + (MN.isMac ? 0 : -3),
arrow === DirectionOfSelection.toLeft
? winRect.y - 82
: winRect.y + 61,
height,
width: winRect.width
}
Expand Down Expand Up @@ -129,7 +119,7 @@ export function actionBarView(type: "card" | "text") {
? view.frame.x + view.frame.width / 2 - width / 2 + gap / 2
: view.frame.x - width / 2 + gap / 2
if (x < 30) return 30
const { width: MNWidth } = MN.studyController.view.bounds
const { width: MNWidth } = MN.studyController.view.frame
if (x > MNWidth - 30 - width) return MNWidth - 30 - width
return x
})()
Expand Down
1 change: 1 addition & 0 deletions packages/ohmymn/src/self.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ declare global {
lastOpenPanel: number
lastClickButton: number
lastReaderViewWidth: number
lastStudyControllerViewWidth: number
}
/**
* Only mainView
Expand Down

0 comments on commit d40fc2b

Please sign in to comment.