Skip to content

Commit

Permalink
Support double click to maxilize window on linux
Browse files Browse the repository at this point in the history
  • Loading branch information
devkanro committed Oct 12, 2023
1 parent 331f8fa commit b0b7446
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 17 deletions.
2 changes: 1 addition & 1 deletion core/src/main/kotlin/org/jetbrains/jewel/IconButton.kt
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ fun IconButton(
.border(style.metrics.borderWidth, border),
contentAlignment = Alignment.Center,
content = {
onBackground(backgroundColor) {
onBackground(background) {
content(buttonState)
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,14 @@ import androidx.compose.ui.window.Window
import androidx.compose.ui.window.WindowPlacement
import androidx.compose.ui.window.WindowState
import androidx.compose.ui.window.rememberWindowState
import com.jetbrains.JBR
import org.jetbrains.jewel.IntelliJTheme
import org.jetbrains.jewel.foundation.Stroke
import org.jetbrains.jewel.foundation.border
import org.jetbrains.jewel.window.styling.DecoratedWindowStyle
import org.jetbrains.jewel.window.utils.DesktopPlatform
import java.awt.event.ComponentEvent
import java.awt.event.ComponentListener
import java.awt.event.WindowAdapter
import java.awt.event.WindowEvent
import javax.swing.JFrame
Expand All @@ -54,6 +57,14 @@ import javax.swing.JFrame
style: DecoratedWindowStyle = IntelliJTheme.defaultDecoratedWindowStyle,
content: @Composable DecoratedWindowScope.() -> Unit,
) {
remember {
if (!JBR.isAvailable()) {
error(
"DecoratedWindow only can be used on JetBrainsRuntime(JBR) platform, please check the document https://github.com/JetBrains/jewel#int-ui-standalone-theme",
)
}
}

// Using undecorated window for linux
val undecorated = DesktopPlatform.Linux == DesktopPlatform.Current

Expand All @@ -75,7 +86,7 @@ import javax.swing.JFrame
var decoratedWindowState by remember { mutableStateOf(DecoratedWindowState.of(window)) }

DisposableEffect(window) {
val adapter = object : WindowAdapter() {
val adapter = object : WindowAdapter(), ComponentListener {
override fun windowActivated(e: WindowEvent?) {
decoratedWindowState = DecoratedWindowState.of(window)
}
Expand All @@ -95,12 +106,27 @@ import javax.swing.JFrame
override fun windowStateChanged(e: WindowEvent) {
decoratedWindowState = DecoratedWindowState.of(window)
}

override fun componentResized(e: ComponentEvent?) {
decoratedWindowState = DecoratedWindowState.of(window)
}

override fun componentMoved(e: ComponentEvent?) {
}

override fun componentShown(e: ComponentEvent?) {
}

override fun componentHidden(e: ComponentEvent?) {
}
}
window.addWindowListener(adapter)
window.addWindowStateListener(adapter)
window.addComponentListener(adapter)
onDispose {
window.removeWindowListener(adapter)
window.removeWindowStateListener(adapter)
window.removeComponentListener(adapter)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import androidx.compose.ui.input.pointer.PointerButton
import androidx.compose.ui.input.pointer.PointerEventPass
import androidx.compose.ui.input.pointer.PointerEventType
import androidx.compose.ui.input.pointer.onPointerEvent
import androidx.compose.ui.platform.LocalViewConfiguration
import androidx.compose.ui.unit.dp
import com.jetbrains.JBR
import org.jetbrains.jewel.Icon
Expand All @@ -31,11 +32,21 @@ import java.awt.event.WindowEvent
style: TitleBarStyle = IntelliJTheme.defaultTitleBarStyle,
content: @Composable TitleBarScope.(DecoratedWindowState) -> Unit,
) {
var lastPress = 0L
val viewConfig = LocalViewConfiguration.current
TitleBarImpl(
modifier.onPointerEvent(PointerEventType.Press, PointerEventPass.Main) {
if (this.currentEvent.button == PointerButton.Primary) {
if (this.currentEvent.changes.any { !it.isConsumed }) {
JBR.getWindowMove().startMovingTogetherWithMouse(window, MouseEvent.BUTTON1)
JBR.getWindowMove()?.startMovingTogetherWithMouse(window, MouseEvent.BUTTON1)
if (System.currentTimeMillis() - lastPress in viewConfig.doubleTapMinTimeMillis..viewConfig.doubleTapTimeoutMillis) {
if (state.isMaximized) {
window.extendedState = Frame.NORMAL
} else {
window.extendedState = Frame.MAXIMIZED_BOTH
}
}
lastPress = System.currentTimeMillis()
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import java.awt.Window
import java.lang.reflect.InvocationTargetException
import java.util.logging.Level
import java.util.logging.Logger
import javax.swing.SwingUtilities

internal object MacUtil {

Expand Down Expand Up @@ -69,22 +70,26 @@ internal object MacUtil {
}

fun updateColors(w: Window) {
val window = MacUtil.getWindowFromJavaWindow(w)
val delegate = Foundation.invoke(window, "delegate")
if (Foundation.invoke(delegate, "respondsToSelector:", Foundation.createSelector("updateColors"))
.booleanValue()
) {
Foundation.invoke(delegate, "updateColors")
SwingUtilities.invokeLater {
val window = MacUtil.getWindowFromJavaWindow(w)
val delegate = Foundation.invoke(window, "delegate")
if (Foundation.invoke(delegate, "respondsToSelector:", Foundation.createSelector("updateColors"))
.booleanValue()
) {
Foundation.invoke(delegate, "updateColors")
}
}
}

fun updateFullScreenButtons(w: Window) {
val selector = Foundation.createSelector("updateFullScreenButtons")
val window = getWindowFromJavaWindow(w)
val delegate = Foundation.invoke(window, "delegate")
SwingUtilities.invokeLater {
val selector = Foundation.createSelector("updateFullScreenButtons")
val window = getWindowFromJavaWindow(w)
val delegate = Foundation.invoke(window, "delegate")

if (Foundation.invoke(delegate, "respondsToSelector:", selector).booleanValue()) {
Foundation.invoke(delegate, "updateFullScreenButtons")
if (Foundation.invoke(delegate, "respondsToSelector:", selector).booleanValue()) {
Foundation.invoke(delegate, "updateFullScreenButtons")
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,13 @@ import org.jetbrains.jewel.window.styling.TitleBarStyle
Color.Transparent,
colors.iconButtonPressBackground,
colors.iconButtonHoverBackground,
Color.Transparent,
Color.Transparent,
Color.Transparent,
Color.Transparent,
Color.Transparent,
),
IntUiIconButtonMetrics(),
IntUiIconButtonMetrics(borderWidth = 0.dp),
)
}

Expand All @@ -52,8 +57,13 @@ import org.jetbrains.jewel.window.styling.TitleBarStyle
Color.Transparent,
colors.titlePaneButtonPressBackground,
colors.titlePaneButtonHoverBackground,
Color.Transparent,
Color.Transparent,
Color.Transparent,
Color.Transparent,
Color.Transparent,
),
IntUiIconButtonMetrics(CornerSize(0.dp)),
IntUiIconButtonMetrics(CornerSize(0.dp), borderWidth = 0.dp),
)
}

Expand All @@ -65,8 +75,13 @@ import org.jetbrains.jewel.window.styling.TitleBarStyle
Color.Transparent,
colors.titlePaneCloseButtonPressBackground,
colors.titlePaneCloseButtonHoverBackground,
Color.Transparent,
Color.Transparent,
Color.Transparent,
Color.Transparent,
Color.Transparent,
),
IntUiIconButtonMetrics(CornerSize(0.dp)),
IntUiIconButtonMetrics(CornerSize(0.dp), borderWidth = 0.dp),
)
}

Expand Down

0 comments on commit b0b7446

Please sign in to comment.