move winows on resize

This commit is contained in:
HF 2021-04-28 22:52:33 +02:00
parent 6d1ad02b26
commit 56d4267cfe
9 changed files with 63 additions and 12 deletions

View File

@ -169,6 +169,14 @@ export function setMobile(mobile: boolean): Action {
}; };
} }
export function windowResize(width: number, height: number): Action {
return {
type: 'WINDOW_RESIZE',
width,
height,
};
}
export function selectColor(color: ColorIndex): Action { export function selectColor(color: ColorIndex): Action {
return { return {
type: 'SELECT_COLOR', type: 'SELECT_COLOR',

View File

@ -35,6 +35,7 @@ export type Action =
| { type: 'SET_WAIT', wait: ?number } | { type: 'SET_WAIT', wait: ?number }
| { type: 'RECEIVE_COOLDOWN', wait: number } | { type: 'RECEIVE_COOLDOWN', wait: number }
| { type: 'SET_MOBILE', mobile: boolean } | { type: 'SET_MOBILE', mobile: boolean }
| { type: 'WINDOW_RESIZE', width: number, height: number }
| { type: 'COOLDOWN_END' } | { type: 'COOLDOWN_END' }
| { type: 'COOLDOWN_SET', coolDown: number } | { type: 'COOLDOWN_SET', coolDown: number }
| { type: 'COOLDOWN_DELTA', delta: number } | { type: 'COOLDOWN_DELTA', delta: number }

View File

@ -17,6 +17,7 @@ import {
addChatChannel, addChatChannel,
removeChatChannel, removeChatChannel,
setMobile, setMobile,
windowResize,
} from './actions'; } from './actions';
import { import {
receivePixelUpdate, receivePixelUpdate,
@ -93,6 +94,14 @@ function init() {
} }
document.addEventListener('touchstart', checkMobile, { once: true }); document.addEventListener('touchstart', checkMobile, { once: true });
// listen for resize
//
function onWindowResize() {
store.dispatch(windowResize(window.innerWidth, window.innerHeight));
}
window.addEventListener('resize', onWindowResize);
onWindowResize();
store.dispatch(initTimer()); store.dispatch(initTimer());
store.dispatch(fetchMe()); store.dispatch(fetchMe());

View File

@ -36,14 +36,11 @@ const ChannelContextMenu = ({
close(); close();
} }
}; };
const handleWindowResize = () => close();
document.addEventListener('mousedown', handleClickOutside); document.addEventListener('mousedown', handleClickOutside);
document.addEventListener('touchstart', handleClickOutside); document.addEventListener('touchstart', handleClickOutside);
window.addEventListener('resize', handleWindowResize);
return () => { return () => {
document.removeEventListener('mousedown', handleClickOutside); document.removeEventListener('mousedown', handleClickOutside);
document.removeEventListener('touchstart', handleClickOutside); document.removeEventListener('touchstart', handleClickOutside);
window.removeEventListener('resize', handleWindowResize);
}; };
}, [wrapperRef]); }, [wrapperRef]);

View File

@ -37,14 +37,11 @@ const UserContextMenu = ({
close(); close();
} }
}; };
const handleWindowResize = () => close();
document.addEventListener('mousedown', handleClickOutside); document.addEventListener('mousedown', handleClickOutside);
document.addEventListener('touchstart', handleClickOutside); document.addEventListener('touchstart', handleClickOutside);
window.addEventListener('resize', handleWindowResize);
return () => { return () => {
document.removeEventListener('mousedown', handleClickOutside); document.removeEventListener('mousedown', handleClickOutside);
document.removeEventListener('touchstart', handleClickOutside); document.removeEventListener('touchstart', handleClickOutside);
window.removeEventListener('resize', handleWindowResize);
}; };
}, [wrapperRef]); }, [wrapperRef]);

View File

@ -42,7 +42,6 @@ const Window = ({ id }) => {
document.removeEventListener('mousemove', move); document.removeEventListener('mousemove', move);
}; };
document.addEventListener('mouseup', stopMove, { once: true }); document.addEventListener('mouseup', stopMove, { once: true });
document.addEventListener('mouseleave', stopMove, { once: true });
}, []); }, []);
const startResize = useCallback((event) => { const startResize = useCallback((event) => {
@ -65,7 +64,6 @@ const Window = ({ id }) => {
document.removeEventListener('mousemove', resize); document.removeEventListener('mousemove', resize);
}; };
document.addEventListener('mouseup', stopResize, { once: true }); document.addEventListener('mouseup', stopResize, { once: true });
document.addEventListener('mouseleave', stopResize, { once: true });
}, []); }, []);
if (!win) { if (!win) {

View File

@ -44,6 +44,7 @@ export default function contextMenu(
}; };
} }
case 'WINDOW_RESIZE':
case 'HIDE_CONTEXT_MENU': { case 'HIDE_CONTEXT_MENU': {
return { return {
...state, ...state,

View File

@ -6,6 +6,8 @@
import type { Action } from '../actions/types'; import type { Action } from '../actions/types';
import { clamp } from '../core/utils';
function generateWindowId(state) { function generateWindowId(state) {
let windowId = Math.floor(Math.random() * 99999) + 1; let windowId = Math.floor(Math.random() * 99999) + 1;
while (state.args[windowId]) { while (state.args[windowId]) {
@ -254,12 +256,16 @@ export default function windows(
xDiff, xDiff,
yDiff, yDiff,
} = action; } = action;
const {
innerWidth: width,
innerHeight: height,
} = window;
const newWindows = state.windows.map((win) => { const newWindows = state.windows.map((win) => {
if (win.windowId !== windowId) return win; if (win.windowId !== windowId) return win;
return { return {
...win, ...win,
xPos: win.xPos + xDiff, xPos: clamp(win.xPos + xDiff, -win.width + 70, width - 30),
yPos: win.yPos + yDiff, yPos: clamp(win.yPos + yDiff, 0, height - 30),
}; };
}); });
return { return {
@ -278,8 +284,8 @@ export default function windows(
if (win.windowId !== windowId) return win; if (win.windowId !== windowId) return win;
return { return {
...win, ...win,
width: win.width + xDiff, width: Math.max(win.width + xDiff, 70, 70 - win.xPos),
height: win.height + yDiff, height: Math.max(win.height + yDiff, 50),
}; };
}); });
return { return {
@ -288,6 +294,39 @@ export default function windows(
}; };
} }
case 'WINDOW_RESIZE': {
const {
width,
height,
} = action;
const xMax = width - 30;
const yMax = height - 30;
let modified = false;
const newWindows = [];
for (let i = 0; i < state.windows.length; i += 1) {
const win = state.windows[i];
let { xPos, yPos } = win;
if (xPos > xMax || yPos > yMax) {
modified = true;
newWindows.push({
...win,
xPos: Math.min(xMax, xPos),
yPos: Math.min(yMax, yPos),
});
} else {
newWindows.push(win);
}
}
if (!modified) return state;
return {
...state,
windows: newWindows,
};
}
case 'CLOSE_ALL_WINDOWS': { case 'CLOSE_ALL_WINDOWS': {
return initialState; return initialState;
} }

View File

@ -140,6 +140,7 @@ tr:nth-child(even) {
border: solid black; border: solid black;
border-width: thin; border-width: thin;
overflow: hidden; overflow: hidden;
z-index: 3;
} }
.win-topbar { .win-topbar {