<commit_before>/**
 * @license
 * Copyright 2018 Google Inc.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @file Support for defining user-selectable tools.
 */

import './tool.css';

import debounce from 'lodash/debounce';
import {MouseSelectionState, UserLayer, UserLayerConstructor} from 'neuroglancer/layer';
import {StatusMessage} from 'neuroglancer/status';
import {TrackableValueInterface} from 'neuroglancer/trackable_value';
import {animationFrameDebounce} from 'neuroglancer/util/animation_frame_debounce';
import {Borrowed, Owned, RefCounted} from 'neuroglancer/util/disposable';
import {ActionEvent, EventActionMap, registerActionListener} from 'neuroglancer/util/event_action_map';
import {verifyObject, verifyObjectProperty, verifyString} from 'neuroglancer/util/json';
import {AnyConstructor} from 'neuroglancer/util/mixin';
import {Signal} from 'neuroglancer/util/signal';

const TOOL_KEY_PATTERN = /^[A-Z]$/;

export type InputEventMapBinder = (eventActionMap: EventActionMap, context: RefCounted) => void;

export class ToolActivation<ToolType extends Tool = Tool> extends RefCounted {
  constructor(public tool: ToolType, public inputEventMapBinder: InputEventMapBinder) {
    super();
  }
  bindAction<Info>(action: string, listener: (event: ActionEvent<Info>) => void) {
    this.registerDisposer(registerActionListener(window, action, listener));
  }
  bindInputEventMap(inputEventMap: EventActionMap) {
    this.inputEventMapBinder(inputEventMap, this);
  }
}

export abstract class Tool<LayerType extends UserLayer = UserLayer> extends RefCounted {
  changed = new Signal();
  keyBinding: string|undefined = undefined;
  constructor(public layer: LayerType) {
    super();
  }
  get mouseState() {
    return this.layer.manager.root.layerSelectedValues.mouseState;
  }
  abstract activate(activation: ToolActivation<this>): void;
  abstract toJSON(): any;
  deactivate(): void {}
  abstract description: string;
  unbind() {
    const {layer} = this;
    const {keyBinding} = this;
    if (keyBinding !== undefined) {
      layer.toolBinder.set(keyBinding, undefined);
    }
  }
}

export abstract class LegacyTool<LayerType extends UserLayer = UserLayer> extends RefCounted {
  changed = new Signal();
  constructor(public layer: LayerType) {
    super();<commit_msg>fix(ui/tool): remove unused Tool.deactivate method<commit_after>/**
 * @license
 * Copyright 2018 Google Inc.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @file Support for defining user-selectable tools.
 */

import './tool.css';

import debounce from 'lodash/debounce';
import {MouseSelectionState, UserLayer, UserLayerConstructor} from 'neuroglancer/layer';
import {StatusMessage} from 'neuroglancer/status';
import {TrackableValueInterface} from 'neuroglancer/trackable_value';
import {animationFrameDebounce} from 'neuroglancer/util/animation_frame_debounce';
import {Borrowed, Owned, RefCounted} from 'neuroglancer/util/disposable';
import {ActionEvent, EventActionMap, registerActionListener} from 'neuroglancer/util/event_action_map';
import {verifyObject, verifyObjectProperty, verifyString} from 'neuroglancer/util/json';
import {AnyConstructor} from 'neuroglancer/util/mixin';
import {Signal} from 'neuroglancer/util/signal';

const TOOL_KEY_PATTERN = /^[A-Z]$/;

export type InputEventMapBinder = (eventActionMap: EventActionMap, context: RefCounted) => void;

export class ToolActivation<ToolType extends Tool = Tool> extends RefCounted {
  constructor(public tool: ToolType, public inputEventMapBinder: InputEventMapBinder) {
    super();
  }
  bindAction<Info>(action: string, listener: (event: ActionEvent<Info>) => void) {
    this.registerDisposer(registerActionListener(window, action, listener));
  }
  bindInputEventMap(inputEventMap: EventActionMap) {
    this.inputEventMapBinder(inputEventMap, this);
  }
}

export abstract class Tool<LayerType extends UserLayer = UserLayer> extends RefCounted {
  changed = new Signal();
  keyBinding: string|undefined = undefined;
  constructor(public layer: LayerType) {
    super();
  }
  get mouseState() {
    return this.layer.manager.root.layerSelectedValues.mouseState;
  }
  abstract activate(activation: ToolActivation<this>): void;
  abstract toJSON(): any;
  abstract description: string;
  unbind() {
    const {layer} = this;
    const {keyBinding} = this;
    if (keyBinding !== undefined) {
      layer.toolBinder.set(keyBinding, undefined);
    }
  }
}

export abstract class LegacyTool<LayerType extends UserLayer = UserLayer> extends RefCounted {
  changed = new Signal();
  constructor(public layer: LayerType) {
    super();
{'fraction_non_alphanumeric': 0.10281864917567807, 'fraction_numerical': 0.0028363765289842226, 'mean_word_length': 5.1593886462882095, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1525248', 'n_tokens_mistral': 1624, 'n_tokens_neox': 1587, 'n_words': 485}