Skip to content

Commit

Permalink
feat(editor): add color scheme item and default night colors for edit…
Browse files Browse the repository at this point in the history
…or text action window (close #644)
  • Loading branch information
Rosemoe committed Jan 31, 2025
1 parent 3685286 commit 59a882e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
package io.github.rosemoe.sora.widget.component;

import android.annotation.SuppressLint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.RectF;
import android.graphics.drawable.GradientDrawable;
import android.view.LayoutInflater;
Expand All @@ -34,6 +36,7 @@
import androidx.annotation.NonNull;

import io.github.rosemoe.sora.R;
import io.github.rosemoe.sora.event.ColorSchemeUpdateEvent;
import io.github.rosemoe.sora.event.EditorFocusChangeEvent;
import io.github.rosemoe.sora.event.EditorReleaseEvent;
import io.github.rosemoe.sora.event.EventManager;
Expand All @@ -45,6 +48,7 @@
import io.github.rosemoe.sora.widget.CodeEditor;
import io.github.rosemoe.sora.widget.EditorTouchEventHandler;
import io.github.rosemoe.sora.widget.base.EditorPopupWindow;
import io.github.rosemoe.sora.widget.schemes.EditorColorScheme;

/**
* This window will show when selecting text to present text actions.
Expand All @@ -55,6 +59,7 @@ public class EditorTextActionWindow extends EditorPopupWindow implements View.On
private final static long DELAY = 200;
private final static long CHECK_FOR_DISMISS_INTERVAL = 100;
private final CodeEditor editor;
private final ImageButton selectAllBtn;
private final ImageButton pasteBtn;
private final ImageButton copyBtn;
private final ImageButton cutBtn;
Expand All @@ -81,36 +86,59 @@ public EditorTextActionWindow(CodeEditor editor) {
// Since popup window does provide decor view, we have to pass null to this method
@SuppressLint("InflateParams")
View root = this.rootView = LayoutInflater.from(editor.getContext()).inflate(R.layout.text_compose_panel, null);
ImageButton selectAll = root.findViewById(R.id.panel_btn_select_all);
selectAllBtn = root.findViewById(R.id.panel_btn_select_all);
cutBtn = root.findViewById(R.id.panel_btn_cut);
copyBtn = root.findViewById(R.id.panel_btn_copy);
longSelectBtn = root.findViewById(R.id.panel_btn_long_select);
pasteBtn = root.findViewById(R.id.panel_btn_paste);

selectAll.setOnClickListener(this);
selectAllBtn.setOnClickListener(this);
cutBtn.setOnClickListener(this);
copyBtn.setOnClickListener(this);
pasteBtn.setOnClickListener(this);
longSelectBtn.setOnClickListener(this);

GradientDrawable gd = new GradientDrawable();
gd.setCornerRadius(5 * editor.getDpUnit());
gd.setColor(0xffffffff);
root.setBackground(gd);
applyColorScheme();
setContentView(root);
setSize(0, (int) (this.editor.getDpUnit() * 48));
getPopup().setAnimationStyle(R.style.text_action_popup_animation);

subscribeEvents();
}

protected void applyColorFilter(ImageButton btn, int color) {
var drawable = btn.getDrawable();
if (drawable == null) {
return;
}
btn.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_ATOP));
}

protected void applyColorScheme() {
GradientDrawable gd = new GradientDrawable();
gd.setCornerRadius(5 * editor.getDpUnit());
gd.setColor(editor.getColorScheme().getColor(EditorColorScheme.TEXT_ACTION_WINDOW_BACKGROUND));
rootView.setBackground(gd);
int color = editor.getColorScheme().getColor(EditorColorScheme.TEXT_ACTION_WINDOW_ICON_COLOR);
applyColorFilter(selectAllBtn, color);
applyColorFilter(cutBtn, color);
applyColorFilter(copyBtn, color);
applyColorFilter(pasteBtn, color);
applyColorFilter(longSelectBtn, color);
}

protected void subscribeEvents() {
eventManager.subscribeAlways(SelectionChangeEvent.class, this::onSelectionChange);
eventManager.subscribeAlways(ScrollEvent.class, this::onEditorScroll);
eventManager.subscribeAlways(HandleStateChangeEvent.class, this::onHandleStateChange);
eventManager.subscribeAlways(LongPressEvent.class, this::onEditorLongPress);
eventManager.subscribeAlways(EditorFocusChangeEvent.class, this::onEditorFocusChange);
eventManager.subscribeAlways(EditorReleaseEvent.class, this::onEditorRelease);
eventManager.subscribeAlways(ColorSchemeUpdateEvent.class, this::onEditorColorChange);
}

protected void onEditorColorChange(@NonNull ColorSchemeUpdateEvent event) {
applyColorScheme();
}

protected void onEditorFocusChange(@NonNull EditorFocusChangeEvent event) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package io.github.rosemoe.sora.widget.schemes;

import android.graphics.Color;
import android.util.SparseIntArray;

import androidx.annotation.NonNull;
Expand Down Expand Up @@ -149,11 +150,6 @@ public class EditorColorScheme {
public static final int LINE_NUMBER = 2;
public static final int LINE_DIVIDER = 1;

/**
* Min pre-defined color id
*/
protected static final int START_COLOR_ID = 1;

public static final int SIGNATURE_TEXT_NORMAL = 58;
public static final int SIGNATURE_TEXT_HIGHLIGHTED_PARAMETER = 59;

Expand All @@ -162,10 +158,18 @@ public class EditorColorScheme {

public static final int SIGNATURE_BACKGROUND = 60;

public static final int TEXT_ACTION_WINDOW_BACKGROUND = 65;
public static final int TEXT_ACTION_WINDOW_ICON_COLOR = 66;

/**
* Min pre-defined color id
*/
protected static final int START_COLOR_ID = 1;

/**
* Max pre-defined color id
*/
protected static final int END_COLOR_ID = 64;
protected static final int END_COLOR_ID = 66;


/**
Expand Down Expand Up @@ -404,8 +408,12 @@ private void applyDefault(int type) {
break;
case SIGNATURE_BACKGROUND:
case DIAGNOSTIC_TOOLTIP_BACKGROUND:
case TEXT_ACTION_WINDOW_BACKGROUND:
color = isDark() ? BACKGROUND_COLOR_DARK : BACKGROUND_COLOR_LIGHT;
break;
case TEXT_ACTION_WINDOW_ICON_COLOR:
color = isDark() ? 0xffeeeeee : Color.GRAY;
break;
case DIAGNOSTIC_TOOLTIP_ACTION:
color = 0xff42A5F5;
}
Expand Down
1 change: 0 additions & 1 deletion editor/src/main/res/layout/text_compose_panel.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center_vertical"
android:background="@drawable/magnifier_background"
android:id="@+id/panel_root">

<HorizontalScrollView
Expand Down

0 comments on commit 59a882e

Please # to comment.