1
0
forked from mirror/misskey
This commit is contained in:
こぴなたみぽ 2018-02-11 22:04:08 +09:00
parent 6cf1fdbf82
commit a0f3182a0a
2 changed files with 361 additions and 341 deletions

View File

@ -79,6 +79,11 @@ export default class MiOS extends EventEmitter {
*/
private shouldRegisterSw: boolean;
/**
*
*/
public windows = new WindowSystem();
/**
* MiOSインスタンスを作成します
* @param shouldRegisterSw ServiceWorkerを登録するかどうか
@ -359,6 +364,22 @@ export default class MiOS extends EventEmitter {
}
}
class WindowSystem {
private windows = new Set();
public add(window) {
this.windows.add(window);
}
public remove(window) {
this.windows.delete(window);
}
public getAll() {
return this.windows;
}
}
/**
* Convert the URL safe base64 string to a Uint8Array
* @param base64String base64 string

View File

@ -3,23 +3,23 @@
<div class="bg" ref="bg" v-show="isModal" @click="onBgClick"></div>
<div class="main" ref="main" tabindex="-1" :data-is-modal="isModal" @mousedown="onBodyMousedown" @keydown="onKeydown" :style="{ width, height }">
<div class="body">
<header ref="header" @mousedown="onHeaderMousedown">
<header ref="header" @contextmenu.prevent="() => {}" @mousedown.prevent="onHeaderMousedown">
<h1 data-yield="header"><yield from="header"/></h1>
<div>
<button class="popout" v-if="popoutUrl" @mousedown="repelMove" @click="popout" title="ポップアウト">%fa:R window-restore%</button>
<button class="close" v-if="canClose" @mousedown="repelMove" @click="close" title="閉じる">%fa:times%</button>
<button class="popout" v-if="popoutUrl" @mousedown.stop="() => {}" @click="popout" title="ポップアウト">%fa:R window-restore%</button>
<button class="close" v-if="canClose" @mousedown.stop="() => {}" @click="close" title="閉じる">%fa:times%</button>
</div>
</header>
<div class="content" data-yield="content"><yield from="content"/></div>
</div>
<div class="handle top" v-if="canResize" @mousedown="onTopHandleMousedown"></div>
<div class="handle right" v-if="canResize" @mousedown="onRightHandleMousedown"></div>
<div class="handle bottom" v-if="canResize" @mousedown="onBottomHandleMousedown"></div>
<div class="handle left" v-if="canResize" @mousedown="onLeftHandleMousedown"></div>
<div class="handle top-left" v-if="canResize" @mousedown="onTopLeftHandleMousedown"></div>
<div class="handle top-right" v-if="canResize" @mousedown="onTopRightHandleMousedown"></div>
<div class="handle bottom-right" v-if="canResize" @mousedown="onBottomRightHandleMousedown"></div>
<div class="handle bottom-left" v-if="canResize" @mousedown="onBottomLeftHandleMousedown"></div>
<div class="handle top" v-if="canResize" @mousedown.prevent="onTopHandleMousedown"></div>
<div class="handle right" v-if="canResize" @mousedown.prevent="onRightHandleMousedown"></div>
<div class="handle bottom" v-if="canResize" @mousedown.prevent="onBottomHandleMousedown"></div>
<div class="handle left" v-if="canResize" @mousedown.prevent="onLeftHandleMousedown"></div>
<div class="handle top-left" v-if="canResize" @mousedown.prevent="onTopLeftHandleMousedown"></div>
<div class="handle top-right" v-if="canResize" @mousedown.prevent="onTopRightHandleMousedown"></div>
<div class="handle bottom-right" v-if="canResize" @mousedown.prevent="onBottomRightHandleMousedown"></div>
<div class="handle bottom-left" v-if="canResize" @mousedown.prevent="onBottomLeftHandleMousedown"></div>
</div>
</div>
</template>
@ -32,6 +32,18 @@ import contains from '../../common/scripts/contains';
const minHeight = 40;
const minWidth = 200;
function dragListen(fn) {
window.addEventListener('mousemove', fn);
window.addEventListener('mouseleave', dragClear.bind(null, fn));
window.addEventListener('mouseup', dragClear.bind(null, fn));
}
function dragClear(fn) {
window.removeEventListener('mousemove', fn);
window.removeEventListener('mouseleave', dragClear);
window.removeEventListener('mouseup', dragClear);
}
export default Vue.extend({
props: {
isModal: {
@ -54,6 +66,7 @@ export default Vue.extend({
type: String
}
},
computed: {
isFlexible(): boolean {
return this.height == null;
@ -62,109 +75,84 @@ export default Vue.extend({
return !this.isFlexible;
}
},
created() {
//
this.$root.$data.os.windows.add(this);
},
mounted() {
}
});
</script>
<script lang="typescript">
this.on('mount', () => {
this.$refs.main.style.top = '15%';
this.$refs.main.style.left = (window.innerWidth / 2) - (this.$refs.main.offsetWidth / 2) + 'px';
this.$refs.header.addEventListener('contextmenu', e => {
e.preventDefault();
});
const main = this.$refs.main as any;
main.style.top = '15%';
main.style.left = (window.innerWidth / 2) - (main.offsetWidth / 2) + 'px';
window.addEventListener('resize', this.onBrowserResize);
this.open();
});
},
destroyed() {
//
this.$root.$data.os.windows.remove(this);
this.on('unmount', () => {
window.removeEventListener('resize', this.onBrowserResize);
});
},
this.onBrowserResize = () => {
const position = this.$refs.main.getBoundingClientRect();
const browserWidth = window.innerWidth;
const browserHeight = window.innerHeight;
const windowWidth = this.$refs.main.offsetWidth;
const windowHeight = this.$refs.main.offsetHeight;
if (position.left < 0) this.$refs.main.style.left = 0;
if (position.top < 0) this.$refs.main.style.top = 0;
if (position.left + windowWidth > browserWidth) this.$refs.main.style.left = browserWidth - windowWidth + 'px';
if (position.top + windowHeight > browserHeight) this.$refs.main.style.top = browserHeight - windowHeight + 'px';
};
this.open = () => {
methods: {
open() {
this.$emit('opening');
this.top();
const bg = this.$refs.bg as any;
const main = this.$refs.main as any;
if (this.isModal) {
this.$refs.bg.style.pointerEvents = 'auto';
bg.style.pointerEvents = 'auto';
anime({
targets: this.$refs.bg,
targets: bg,
opacity: 1,
duration: 100,
easing: 'linear'
});
}
this.$refs.main.style.pointerEvents = 'auto';
main.style.pointerEvents = 'auto';
anime({
targets: this.$refs.main,
targets: main,
opacity: 1,
scale: [1.1, 1],
duration: 200,
easing: 'easeOutQuad'
});
//this.$refs.main.focus();
if (focus) main.focus();
setTimeout(() => {
this.$emit('opened');
}, 300);
};
},
this.popout = () => {
const position = this.$refs.main.getBoundingClientRect();
const width = parseInt(getComputedStyle(this.$refs.main, '').width, 10);
const height = parseInt(getComputedStyle(this.$refs.main, '').height, 10);
const x = window.screenX + position.left;
const y = window.screenY + position.top;
const url = typeof this.popoutUrl == 'function' ? this.popoutUrl() : this.popoutUrl;
window.open(url, url,
`height=${height},width=${width},left=${x},top=${y}`);
this.close();
};
this.close = () => {
close() {
this.$emit('closing');
const bg = this.$refs.bg as any;
const main = this.$refs.main as any;
if (this.isModal) {
this.$refs.bg.style.pointerEvents = 'none';
bg.style.pointerEvents = 'none';
anime({
targets: this.$refs.bg,
targets: bg,
opacity: 0,
duration: 300,
easing: 'linear'
});
}
this.$refs.main.style.pointerEvents = 'none';
main.style.pointerEvents = 'none';
anime({
targets: this.$refs.main,
targets: main,
opacity: 0,
scale: 0.8,
duration: 300,
@ -174,46 +162,57 @@ this.close = () => {
setTimeout(() => {
this.$emit('closed');
}, 300);
};
},
//
this.top = () => {
popout() {
const main = this.$refs.main as any;
const position = main.getBoundingClientRect();
const width = parseInt(getComputedStyle(main, '').width, 10);
const height = parseInt(getComputedStyle(main, '').height, 10);
const x = window.screenX + position.left;
const y = window.screenY + position.top;
const url = typeof this.popoutUrl == 'function' ? this.popoutUrl() : this.popoutUrl;
window.open(url, url,
`height=${height}, width=${width}, left=${x}, top=${y}`);
this.close();
},
//
top() {
let z = 0;
const ws = document.querySelectorAll('mk-window');
ws.forEach(w => {
if (w == this.root) return;
const m = w.querySelector(':scope > .main');
this.$root.$data.os.windows.getAll().forEach(w => {
if (w == this) return;
const m = w.$refs.main;
const mz = Number(document.defaultView.getComputedStyle(m, null).zIndex);
if (mz > z) z = mz;
});
if (z > 0) {
this.$refs.main.style.zIndex = z + 1;
if (this.isModal) this.$refs.bg.style.zIndex = z + 1;
(this.$refs.main as any).style.zIndex = z + 1;
if (this.isModal) (this.$refs.bg as any).style.zIndex = z + 1;
}
};
},
this.repelMove = e => {
e.stopPropagation();
return true;
};
this.bgClick = () => {
onBgClick() {
if (this.canClose) this.close();
};
},
this.onBodyMousedown = () => {
onBodyMousedown() {
this.top();
};
},
//
this.onHeaderMousedown = e => {
e.preventDefault();
onHeaderMousedown(e) {
const main = this.$refs.main as any;
if (!contains(this.$refs.main, document.activeElement)) this.$refs.main.focus();
if (!contains(main, document.activeElement)) main.focus();
const position = this.$refs.main.getBoundingClientRect();
const position = main.getBoundingClientRect();
const clickX = e.clientX;
const clickY = e.clientY;
@ -221,8 +220,8 @@ this.onHeaderMousedown = e => {
const moveBaseY = clickY - position.top;
const browserWidth = window.innerWidth;
const browserHeight = window.innerHeight;
const windowWidth = this.$refs.main.offsetWidth;
const windowHeight = this.$refs.main.offsetHeight;
const windowWidth = main.offsetWidth;
const windowHeight = main.offsetHeight;
//
dragListen(me => {
@ -241,172 +240,160 @@ this.onHeaderMousedown = e => {
//
if (moveLeft + windowWidth > browserWidth) moveLeft = browserWidth - windowWidth;
this.$refs.main.style.left = moveLeft + 'px';
this.$refs.main.style.top = moveTop + 'px';
main.style.left = moveLeft + 'px';
main.style.top = moveTop + 'px';
});
};
},
//
this.onTopHandleMousedown = e => {
e.preventDefault();
onTopHandleMousedown(e) {
const main = this.$refs.main as any;
const base = e.clientY;
const height = parseInt(getComputedStyle(this.$refs.main, '').height, 10);
const top = parseInt(getComputedStyle(this.$refs.main, '').top, 10);
const height = parseInt(getComputedStyle(main, '').height, 10);
const top = parseInt(getComputedStyle(main, '').top, 10);
//
dragListen(me => {
const move = me.clientY - base;
if (top + move > 0) {
if (height + -move > this.minHeight) {
if (height + -move > minHeight) {
this.applyTransformHeight(height + -move);
this.applyTransformTop(top + move);
} else { //
this.applyTransformHeight(this.minHeight);
this.applyTransformTop(top + (height - this.minHeight));
this.applyTransformHeight(minHeight);
this.applyTransformTop(top + (height - minHeight));
}
} else { //
this.applyTransformHeight(top + height);
this.applyTransformTop(0);
}
});
};
},
//
this.onRightHandleMousedown = e => {
e.preventDefault();
onRightHandleMousedown(e) {
const main = this.$refs.main as any;
const base = e.clientX;
const width = parseInt(getComputedStyle(this.$refs.main, '').width, 10);
const left = parseInt(getComputedStyle(this.$refs.main, '').left, 10);
const width = parseInt(getComputedStyle(main, '').width, 10);
const left = parseInt(getComputedStyle(main, '').left, 10);
const browserWidth = window.innerWidth;
//
dragListen(me => {
const move = me.clientX - base;
if (left + width + move < browserWidth) {
if (width + move > this.minWidth) {
if (width + move > minWidth) {
this.applyTransformWidth(width + move);
} else { //
this.applyTransformWidth(this.minWidth);
this.applyTransformWidth(minWidth);
}
} else { //
this.applyTransformWidth(browserWidth - left);
}
});
};
},
//
this.onBottomHandleMousedown = e => {
e.preventDefault();
onBottomHandleMousedown(e) {
const main = this.$refs.main as any;
const base = e.clientY;
const height = parseInt(getComputedStyle(this.$refs.main, '').height, 10);
const top = parseInt(getComputedStyle(this.$refs.main, '').top, 10);
const height = parseInt(getComputedStyle(main, '').height, 10);
const top = parseInt(getComputedStyle(main, '').top, 10);
const browserHeight = window.innerHeight;
//
dragListen(me => {
const move = me.clientY - base;
if (top + height + move < browserHeight) {
if (height + move > this.minHeight) {
if (height + move > minHeight) {
this.applyTransformHeight(height + move);
} else { //
this.applyTransformHeight(this.minHeight);
this.applyTransformHeight(minHeight);
}
} else { //
this.applyTransformHeight(browserHeight - top);
}
});
};
},
//
this.onLeftHandleMousedown = e => {
e.preventDefault();
onLeftHandleMousedown(e) {
const main = this.$refs.main as any;
const base = e.clientX;
const width = parseInt(getComputedStyle(this.$refs.main, '').width, 10);
const left = parseInt(getComputedStyle(this.$refs.main, '').left, 10);
const width = parseInt(getComputedStyle(main, '').width, 10);
const left = parseInt(getComputedStyle(main, '').left, 10);
//
dragListen(me => {
const move = me.clientX - base;
if (left + move > 0) {
if (width + -move > this.minWidth) {
if (width + -move > minWidth) {
this.applyTransformWidth(width + -move);
this.applyTransformLeft(left + move);
} else { //
this.applyTransformWidth(this.minWidth);
this.applyTransformLeft(left + (width - this.minWidth));
this.applyTransformWidth(minWidth);
this.applyTransformLeft(left + (width - minWidth));
}
} else { //
this.applyTransformWidth(left + width);
this.applyTransformLeft(0);
}
});
};
},
//
this.onTopLeftHandleMousedown = e => {
onTopLeftHandleMousedown(e) {
this.onTopHandleMousedown(e);
this.onLeftHandleMousedown(e);
};
},
//
this.onTopRightHandleMousedown = e => {
onTopRightHandleMousedown(e) {
this.onTopHandleMousedown(e);
this.onRightHandleMousedown(e);
};
},
//
this.onBottomRightHandleMousedown = e => {
onBottomRightHandleMousedown(e) {
this.onBottomHandleMousedown(e);
this.onRightHandleMousedown(e);
};
},
//
this.onBottomLeftHandleMousedown = e => {
onBottomLeftHandleMousedown(e) {
this.onBottomHandleMousedown(e);
this.onLeftHandleMousedown(e);
};
},
//
this.applyTransformHeight = height => {
this.$refs.main.style.height = height + 'px';
};
applyTransformHeight(height) {
(this.$refs.main as any).style.height = height + 'px';
},
//
this.applyTransformWidth = width => {
this.$refs.main.style.width = width + 'px';
};
applyTransformWidth(width) {
(this.$refs.main as any).style.width = width + 'px';
},
// Y
this.applyTransformTop = top => {
this.$refs.main.style.top = top + 'px';
};
applyTransformTop(top) {
(this.$refs.main as any).style.top = top + 'px';
},
// X
this.applyTransformLeft = left => {
this.$refs.main.style.left = left + 'px';
};
applyTransformLeft(left) {
(this.$refs.main as any).style.left = left + 'px';
},
function dragListen(fn) {
window.addEventListener('mousemove', fn);
window.addEventListener('mouseleave', dragClear.bind(null, fn));
window.addEventListener('mouseup', dragClear.bind(null, fn));
}
function dragClear(fn) {
window.removeEventListener('mousemove', fn);
window.removeEventListener('mouseleave', dragClear);
window.removeEventListener('mouseup', dragClear);
}
this.ondragover = e => {
onDragover(e) {
e.dataTransfer.dropEffect = 'none';
};
},
this.onKeydown = e => {
onKeydown(e) {
if (e.which == 27) { // Esc
if (this.canClose) {
e.preventDefault();
@ -414,11 +401,24 @@ this.onKeydown = e => {
this.close();
}
}
};
},
onBrowserResize() {
const main = this.$refs.main as any;
const position = main.getBoundingClientRect();
const browserWidth = window.innerWidth;
const browserHeight = window.innerHeight;
const windowWidth = main.offsetWidth;
const windowHeight = main.offsetHeight;
if (position.left < 0) main.style.left = 0;
if (position.top < 0) main.style.top = 0;
if (position.left + windowWidth > browserWidth) main.style.left = browserWidth - windowWidth + 'px';
if (position.top + windowHeight > browserHeight) main.style.top = browserHeight - windowHeight + 'px';
}
}
});
</script>
<style lang="stylus" scoped>
.mk-window
display block
@ -584,4 +584,3 @@ this.onKeydown = e => {
height calc(100% - 40px)
</style>