Feature: Tab switcher

This commit is contained in:
zt515 2017-06-11 21:53:48 +08:00
parent acff62db44
commit 6599700d64
7 changed files with 305 additions and 117 deletions

View File

@ -15,6 +15,7 @@ android {
externalNativeBuild { externalNativeBuild {
cmake { cmake {
cppFlags "-std=c++11" cppFlags "-std=c++11"
abiFilters 'x86', 'x86_64', 'armeabi-v7a', 'arm64-v8a'
} }
} }
} }

View File

@ -3,8 +3,8 @@
package="io.neoterm"> package="io.neoterm">
<application <application
android:extractNativeLibs="true"
android:allowBackup="true" android:allowBackup="true"
android:extractNativeLibs="true"
android:icon="@mipmap/ic_launcher" android:icon="@mipmap/ic_launcher"
android:label="@string/app_name" android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round" android:roundIcon="@mipmap/ic_launcher_round"
@ -13,6 +13,7 @@
<activity <activity
android:name=".MainActivity" android:name=".MainActivity"
android:launchMode="singleTask" android:launchMode="singleTask"
android:theme="@style/AppTheme.NoActionBar"
android:windowSoftInputMode="adjustResize|stateAlwaysVisible"> android:windowSoftInputMode="adjustResize|stateAlwaysVisible">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />

View File

@ -1,131 +1,131 @@
package io.neoterm package io.neoterm
import android.app.Activity
import android.content.Context
import android.graphics.Color
import android.graphics.Typeface
import android.os.Bundle import android.os.Bundle
import android.view.InputDevice import android.support.v4.content.ContextCompat
import android.view.KeyEvent import android.support.v4.view.OnApplyWindowInsetsListener
import android.view.MotionEvent import android.support.v4.view.ViewCompat
import android.view.inputmethod.InputMethodManager import android.support.v7.app.AppCompatActivity
import io.neoterm.terminal.TerminalSession import android.support.v7.widget.Toolbar
import io.neoterm.view.ExtraKeysView import android.view.View
import io.neoterm.view.TerminalView import android.widget.ImageButton
import io.neoterm.view.TerminalViewClient import de.mrapp.android.tabswitcher.*
import io.neoterm.tab.TermTabDecorator
class MainActivity : Activity() {
private lateinit var extraKeysView: ExtraKeysView class MainActivity : AppCompatActivity() {
lateinit var tabSwitcher: TabSwitcher
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main) setContentView(R.layout.tab_main)
extraKeysView = findViewById(R.id.extra_keys) as ExtraKeysView tabSwitcher = findViewById(R.id.tab_switcher) as TabSwitcher
val view = findViewById(R.id.terminal_view) as TerminalView tabSwitcher.decorator = TermTabDecorator(this)
ViewCompat.setOnApplyWindowInsetsListener(tabSwitcher, createWindowInsetsListener())
view.setBackgroundColor(Color.BLACK) tabSwitcher.showToolbars(true)
view.textSize = 30 tabSwitcher
view.setTypeface(Typeface.MONOSPACE) .setToolbarNavigationIcon(R.drawable.ic_add_box_white_24dp, createAddTabListener())
tabSwitcher.inflateToolbarMenu(R.menu.tab_switcher, createToolbarMenuListener())
val session = TerminalSession("/system/bin/sh", "/", tabSwitcher.addListener(object : TabSwitcherListener {
arrayOf("/system/bin/sh"), override fun onSwitcherShown(tabSwitcher: TabSwitcher) {
arrayOf("TERM=screen", "HOME=" + filesDir),
object : TerminalSession.SessionChangedCallback {
override fun onBell(session: TerminalSession?) {
}
override fun onClipboardText(session: TerminalSession?, text: String?) {
}
override fun onColorsChanged(session: TerminalSession?) {
}
override fun onSessionFinished(finishedSession: TerminalSession?) {
}
override fun onTextChanged(changedSession: TerminalSession?) {
view.onScreenUpdated()
}
override fun onTitleChanged(changedSession: TerminalSession?) {
}
})
view.setOnKeyListener(object : TerminalViewClient {
internal var mVirtualControlKeyDown: Boolean = false
internal var mVirtualFnKeyDown: Boolean = false
override fun onScale(scale: Float): Float {
if (scale < 0.9f || scale > 1.1f) {
val increase = scale > 1f
val changedSize = (if (increase) 1 else -1) * 2
view.textSize = view.textSize + changedSize
return 1.0f
}
return scale
} }
override fun onSingleTapUp(e: MotionEvent?) { override fun onSwitcherHidden(tabSwitcher: TabSwitcher) {
(getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager)
.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT)
} }
override fun shouldBackButtonBeMappedToEscape(): Boolean { override fun onSelectionChanged(tabSwitcher: TabSwitcher, selectedTabIndex: Int, selectedTab: Tab?) {
return false
} }
override fun copyModeChanged(copyMode: Boolean) { override fun onTabAdded(tabSwitcher: TabSwitcher, index: Int, tab: Tab, animation: Animation) {
// TODO
} }
override fun onKeyDown(keyCode: Int, e: KeyEvent?, session: TerminalSession?): Boolean { override fun onTabRemoved(tabSwitcher: TabSwitcher, index: Int, tab: Tab, animation: Animation) {
// TODO
return false
} }
override fun onKeyUp(keyCode: Int, e: KeyEvent?): Boolean { override fun onAllTabsRemoved(tabSwitcher: TabSwitcher, tabs: Array<out Tab>, animation: Animation) {
return handleVirtualKeys(keyCode, e, false)
} }
override fun readControlKey(): Boolean {
return extraKeysView.readControlButton() || mVirtualControlKeyDown
}
override fun readAltKey(): Boolean {
return extraKeysView.readAltButton() || mVirtualFnKeyDown
}
override fun onCodePoint(codePoint: Int, ctrlDown: Boolean, session: TerminalSession?): Boolean {
// TODO
return false
}
override fun onLongPress(event: MotionEvent?): Boolean {
// TODO
return false
}
private fun handleVirtualKeys(keyCode: Int, event: KeyEvent?, down: Boolean): Boolean {
if (event == null) {
return false
}
val inputDevice = event.device
if (inputDevice != null && inputDevice.keyboardType == InputDevice.KEYBOARD_TYPE_ALPHABETIC) {
return false
} else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
mVirtualControlKeyDown = down
return true
} else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
mVirtualFnKeyDown = down
return true
}
return false
}
}) })
view.attachSession(session)
} }
fun createAddTabListener(): View.OnClickListener {
return View.OnClickListener {
val index = tabSwitcher.count
val animation = createRevealAnimation()
tabSwitcher.addTab(createTab(index), 0, animation)
}
}
fun createToolbarMenuListener(): Toolbar.OnMenuItemClickListener {
return Toolbar.OnMenuItemClickListener { item ->
when (item.itemId) {
R.id.add_tab_menu_item -> {
val index = tabSwitcher.count
val tab = createTab(index)
if (tabSwitcher.isSwitcherShown) {
tabSwitcher.addTab(tab, 0, createRevealAnimation())
} else {
tabSwitcher.addTab(tab, 0, createPeekAnimation())
}
true
}
else -> false
}
}
}
private fun createTab(index: Int): Tab {
val tab = Tab("Neo Term #" + index)
tab.isCloseable = true
tab.parameters = Bundle()
tab.parameters?.putInt("type", TermTabDecorator.TYPE_NEW)
tab.setBackgroundColor(ContextCompat.getColor(this, R.color.tab_background_color))
tab.setTitleTextColor(ContextCompat.getColor(this, R.color.tab_title_text_color))
return tab
}
private fun createRevealAnimation(): Animation {
var x = 0f
var y = 0f
val view = getNavigationMenuItem()
if (view != null) {
val location = IntArray(2)
view.getLocationInWindow(location)
x = location[0] + view.width / 2f
y = location[1] + view.height / 2f
}
return RevealAnimation.Builder().setX(x).setY(y).create()
}
private fun createPeekAnimation(): Animation {
return PeekAnimation.Builder().setX(tabSwitcher.width / 2f).create()
}
private fun getNavigationMenuItem(): View? {
val toolbars = tabSwitcher.toolbars
if (toolbars != null) {
val toolbar = if (toolbars.size > 1) toolbars[1] else toolbars[0]
val size = toolbar.childCount
(0..size - 1)
.map { toolbar.getChildAt(it) }
.filterIsInstance<ImageButton>()
.forEach { return it }
}
return null
}
private fun createWindowInsetsListener(): OnApplyWindowInsetsListener {
return OnApplyWindowInsetsListener { v, insets ->
tabSwitcher.setPadding(insets.systemWindowInsetLeft,
insets.systemWindowInsetTop, insets.systemWindowInsetRight,
insets.systemWindowInsetBottom)
insets
}
}
} }

View File

@ -0,0 +1,168 @@
package io.neoterm.tab
import android.content.Context
import android.graphics.Color
import android.graphics.Typeface
import android.os.Bundle
import android.support.v7.widget.Toolbar
import android.view.*
import android.view.inputmethod.InputMethodManager
import de.mrapp.android.tabswitcher.Tab
import de.mrapp.android.tabswitcher.TabSwitcher
import de.mrapp.android.tabswitcher.TabSwitcherDecorator
import io.neoterm.MainActivity
import io.neoterm.R
import io.neoterm.terminal.TerminalSession
import io.neoterm.view.ExtraKeysView
import io.neoterm.view.TerminalView
import io.neoterm.view.TerminalViewClient
/**
* @author kiva
*/
class TermTabDecorator(val context: MainActivity) : TabSwitcherDecorator() {
override fun onInflateView(inflater: LayoutInflater, parent: ViewGroup?, viewType: Int): View {
val view = inflater.inflate(R.layout.term, parent, false)
val toolbar = view.findViewById(R.id.terminal_toolbar) as Toolbar
toolbar.inflateMenu(R.menu.tab_switcher)
toolbar.setOnMenuItemClickListener(context.createToolbarMenuListener())
val menu = toolbar.menu
TabSwitcher.setupWithMenu(context.tabSwitcher, menu, {
context.tabSwitcher.showSwitcher()
})
return view
}
override fun onShowTab(context: Context, tabSwitcher: TabSwitcher,
view: View, tab: Tab, index: Int, viewType: Int, savedInstanceState: Bundle?) {
val toolbar = findViewById<Toolbar>(R.id.terminal_toolbar)
toolbar?.title = tab.title
val terminalView = findViewById<TerminalView>(R.id.terminal_view)
val extraKeysView = findViewById<ExtraKeysView>(R.id.extra_keys)
setupTerminalView(terminalView, extraKeysView)
}
private fun setupTerminalView(view: TerminalView?, extraKeysView: ExtraKeysView?) {
if (view == null) {
return
}
view.setBackgroundColor(Color.BLACK)
view.textSize = 30
view.setTypeface(Typeface.MONOSPACE)
val session = TerminalSession("/system/bin/sh", "/",
arrayOf("/system/bin/sh"),
arrayOf("TERM=screen", "HOME=" + context.filesDir),
object : TerminalSession.SessionChangedCallback {
override fun onBell(session: TerminalSession?) {
}
override fun onClipboardText(session: TerminalSession?, text: String?) {
}
override fun onColorsChanged(session: TerminalSession?) {
}
override fun onSessionFinished(finishedSession: TerminalSession?) {
}
override fun onTextChanged(changedSession: TerminalSession?) {
view.onScreenUpdated()
}
override fun onTitleChanged(changedSession: TerminalSession?) {
}
})
view.setOnKeyListener(object : TerminalViewClient {
internal var mVirtualControlKeyDown: Boolean = false
internal var mVirtualFnKeyDown: Boolean = false
override fun onScale(scale: Float): Float {
if (scale < 0.9f || scale > 1.1f) {
val increase = scale > 1f
val changedSize = (if (increase) 1 else -1) * 2
view.textSize = view.textSize + changedSize
return 1.0f
}
return scale
}
override fun onSingleTapUp(e: MotionEvent?) {
(context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager)
.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT)
}
override fun shouldBackButtonBeMappedToEscape(): Boolean {
return false
}
override fun copyModeChanged(copyMode: Boolean) {
// TODO
}
override fun onKeyDown(keyCode: Int, e: KeyEvent?, session: TerminalSession?): Boolean {
// TODO
return false
}
override fun onKeyUp(keyCode: Int, e: KeyEvent?): Boolean {
return handleVirtualKeys(keyCode, e, false)
}
override fun readControlKey(): Boolean {
return (extraKeysView != null && extraKeysView.readControlButton()) || mVirtualControlKeyDown
}
override fun readAltKey(): Boolean {
return (extraKeysView != null && extraKeysView.readAltButton()) || mVirtualFnKeyDown
}
override fun onCodePoint(codePoint: Int, ctrlDown: Boolean, session: TerminalSession?): Boolean {
// TODO
return false
}
override fun onLongPress(event: MotionEvent?): Boolean {
// TODO
return false
}
private fun handleVirtualKeys(keyCode: Int, event: KeyEvent?, down: Boolean): Boolean {
if (event == null) {
return false
}
val inputDevice = event.device
if (inputDevice != null && inputDevice.keyboardType == InputDevice.KEYBOARD_TYPE_ALPHABETIC) {
return false
} else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
mVirtualControlKeyDown = down
return true
} else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
mVirtualFnKeyDown = down
return true
}
return false
}
})
view.attachSession(session)
}
override fun getViewTypeCount(): Int {
return 2
}
override fun getViewType(tab: Tab, index: Int): Int {
return tab.parameters?.getInt("type")!!
}
companion object {
val TYPE_LOADED = 1
val TYPE_NEW = 0
}
}

View File

@ -1,10 +1,17 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent">
android:background="@android:color/black"
tools:context="io.neoterm.MainActivity"> <android.support.v7.widget.Toolbar
android:id="@+id/terminal_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimaryDark"
android:theme="@style/ThemeOverlay.AppCompat.Dark"
android:visibility="visible"
app:popupTheme="@style/ThemeOverlay.AppCompat.Dark" />
<io.neoterm.view.ExtraKeysView <io.neoterm.view.ExtraKeysView
android:id="@+id/extra_keys" android:id="@+id/extra_keys"
@ -12,6 +19,7 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="48dp" android:layout_height="48dp"
android:layout_alignParentBottom="true" android:layout_alignParentBottom="true"
android:background="@color/terminal_background"
android:orientation="horizontal" /> android:orientation="horizontal" />
<io.neoterm.view.TerminalView <io.neoterm.view.TerminalView
@ -19,6 +27,8 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_above="@id/extra_keys" android:layout_above="@id/extra_keys"
android:layout_below="@id/terminal_toolbar"
android:background="@color/terminal_background"
android:fadeScrollbars="true" android:fadeScrollbars="true"
android:focusable="true" android:focusable="true"
android:focusableInTouchMode="true" android:focusableInTouchMode="true"

View File

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<resources> <resources>
<color name="colorPrimary">#3F51B5</color> <color name="colorPrimary">#607D8B</color>
<color name="colorPrimaryDark">#303F9F</color> <color name="colorPrimaryDark">#455A64</color>
<color name="colorAccent">#FF4081</color> <color name="colorAccent">#03A9F4</color>
<color name="terminal_background">#13181a</color>
</resources> </resources>

View File

@ -1,8 +1,15 @@
<resources> <resources>
<!-- Base application theme. --> <!-- Base application theme. -->
<style name="AppTheme" parent="android:Theme.Material.Light.DarkActionBar"> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar" >
<!-- Customize your theme here. --> <item name="colorAccent">@color/colorAccent</item>
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
</style>
<style name="AppTheme.NoActionBar" parent="@style/AppTheme">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style> </style>
</resources> </resources>